0

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?

mad_mosel
  • 35
  • 8

2 Answers2

0

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.

life888888
  • 835
  • 2
  • 2
  • 8
  • Thank you for the detailed aswer. meanwhila a colleague of mine help me doing that using the maven-javadoc-plugin. I think it does the same think like here, only difference is usage from CLI. $mvn install The text does not fit in here so I will post it in a new answer – mad_mosel Mar 16 '23 at 14:08
0

"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

mad_mosel
  • 35
  • 8
  • You asked how to create a jar containing both classes and javadoc. Does this technique create a single jar or 2 ? – nquincampoix Mar 22 '23 at 07:33
  • What I wanted to achieve is, that my java-docs are present when I include a library of mine. So that I just have to press ctrl+Q to read the docs vs read the original files I was confused because I thought to achieve this I should produce a fat.jar My problem was that I didn't knew about mvn install / how libs are managed properly. The solution I stick with is now using maven. To have a template for further projects I hacked down this pom.xml (where still the source code is missing...) to not have to use the CLI but just copy its content into the pom.xml of a new Project. – mad_mosel Mar 24 '23 at 18:12
  • This installs following files in ~/.m2.org.example.moduleName: mylib.jar mylib-javadoc.jar mylib.pom mylib-sources.jar maven-metadata-local.xml _remote.repositories When i include it as dependency in pom.xml of a other repo, I can read the java-doc directly from there – mad_mosel Mar 24 '23 at 18:20