Creation of Java jar library in Intellij which includes Quickdock (doc that is shown when pressing ctrl+Q)

53 Views Asked by At

I want to export my code in a jar so I can reuse it in other projects. The question here is how to include the java-doc i wrote in the code in a way that it is displayed in the project where I use the jar file when displaying Quick documentation lookup (intellij ctrl+Q). How can I achieve that?

I commented the code where I need reference for usage like

 /**
 /* This is the method that is doing ....
 /* @returns further info
 public void bla() { ... }

I managed to export a separate jar file for java-doc and classes using maven. I want the docs to be displayed when I press ctrl+Q. I tried to import the docs manually - it works but this is really annoying. I want to tie them together in a jar containing both.

I've searched a lot of questions in here, the answer's are mostly like: "just don't do that because java is not meant to do so. ... " update: as I know now, that makes sense. I didn't knew how the repo-manegment of maven worked so I was confused on that. For others who are stuck here: the libs are stored in .m2/repository/com.example.lib/1.x/ (at least at my PC) where source.jar and java-doc.jar are stored sperate

But if I include the dependencies from maven remote repository, the docs will be available locally. I will have a quick reference by just pressing ctr+Q. I really want to have this with my own libs because I cant remember code I wrote years ago.

Can someone please help me with that? What should I type in pom.xml?

2

There are 2 best solutions below

1
life888888 On BEST ANSWER

Quick answer

The quick answer is: you don't need to add other settings in pom.xml (if you have no other requirements).

generate 3 files

You only need to generate 3 files (xxx.jar, xxx-sources.jar, xxx-javadoc.jar).

In the state of using the default value, you don't need to add any other additional settings.

Run Command:

mvn clean package source:jar javadoc:jar

Install sources and javadoc jar

Run Command:

mvn install:install-file \
    -Dfile=Hello-1.0-SNAPSHOT.jar \
    -DpomFile=pom.xml \
    -Dsources=Hello-1.0-SNAPSHOT-sources.jar \
    -Djavadoc=Hello-1.0-SNAPSHOT-javadoc.jar \
    -DgroupId=org.example \
    -DartifactId=Hello \
    -Dversion=1.0-SNAPSHOT \
    -Dpackaging=jar \
    -Dclassifier=sources \
    -DgeneratePom=true \
    -DcreateChecksum=true

Example Project

Hello
├── pom.xml
└── src
    └── main
        └── java
            └── org
                └── example
                    └── Hello.java

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Hello</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

Hello.java

package org.example;

/**
 * Hello This is a demo class.
 */
public class Hello {

    /**
     * default constructor
     */
    public Hello(){

    }

    /**
     * echo Demo Method
     * @param msg any string.
     * @return Return the original parameter content you passed in.
     */
    public String echo(String msg){
        return msg;
    }

    /**
     * hello Demo Method
     * @param msg any string.
     * @return Return "HELLO" plus the original parameter content you passed in.
     */
    public String hello(String msg){
        return "HELLO "+msg;
    }
}

## generate 3 files

You only need to generate 3 files (xxx.jar, xxx-sources.jar, xxx-javadoc.jar).

In the state of using the default value, you don't need to add any other additional settings.

Run Command:

mvn clean package source:jar javadoc:jar

Install sources and javadoc jar

Run Command: Copy pom.xml to target dir

copy pom.xml targert/

target dir files

target
├── Hello-1.0-SNAPSHOT.jar
├── Hello-1.0-SNAPSHOT-javadoc.jar
├── Hello-1.0-SNAPSHOT-sources.jar
└── pom.xml

Run Command: install jar and source jar , javadoc jar to maven local repo (~/.m2)

cd target

mvn install:install-file \
    -Dfile=Hello-1.0-SNAPSHOT.jar \
    -DpomFile=pom.xml \
    -Dsources=Hello-1.0-SNAPSHOT-sources.jar \
    -Djavadoc=Hello-1.0-SNAPSHOT-javadoc.jar \
    -DgroupId=org.example \
    -DartifactId=Hello \
    -Dversion=1.0-SNAPSHOT \
    -Dpackaging=jar \
    -Dclassifier=sources \
    -DgeneratePom=true \
    -DcreateChecksum=true

(Ref: https://maven.apache.org/plugins/maven-install-plugin/usage.html )

Test Project - TestHello

TestHello
├── pom.xml
└── src
    └── main
        └── java
            └── org
                └── example
                    └── Main.java

pom.xml

ADD org.example Hello dependency

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>Hello</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

Open TestHello in IDEA

then you can use Ctl+Q , show javadoc, you can use Key F4 Jump to Source.

3
mad_mosel On

"Server" pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mylib</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>



    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
        </plugins>
    </build>

</project>

when running mvn install the output goes to ~/.m2/repository/com/example/mylib

"client" pom.xml

<dependency>
    <groupId>com.example</groupId>
    <artifactId>mylib</artifactId>
    <version>1.0</version>
</dependency>

When pressing ctrl+Q in Intellij, the java-doc is present