I wrote my little library, commented all the methods and variables.

When I compiled jar, connected it to the project, and decompiled, all comments were gone.

How to create a jar with comments? I know about javadoc generation, I just need comments.

  • one
    In maven, it’s customary to make a separate .sources.jar and decent ide know this and show the source code correctly - Stranger in the Q
  • maven-source-plugin - Sergey Gornostaev
  • No All comments are ignored (which is correct. Why are they needed in the final compiled product?) - Enikeyschik
  • But to take the same HashMap class, all comments can be seen - Andriy Ostashevskiy
  • one
    Comments should be written using annotations - Roman C

1 answer 1

When building a Maven project, there is a special plugin that builds javadoc at build time to add to pom.xml

<dependencies> ... <!-- Maven Javadoc Plugin Javadocs provide documentation that makes it easier for developers to know how to use a particular class. Instead of reading and understanding the actual source code, the developer can use the Javadocs instead to lookup the class attributes and methods. --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.0</version> </dependency> ... </dependencies> 

use the plugin when building the project

 <build> ... <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.0</version> <configuration> <docencoding>UTF-8</docencoding> </configuration> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> 

add section

 <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.0</version> <configuration> <docencoding>UTF-8</docencoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jxr-plugin</artifactId> <version>2.5</version> </plugin> </plugins> </reporting> 

this will create a documentation jar file. Further Maven it is necessary to specify to load dependences with the documentation, in Ideas in ideas it looks like this enter image description here

A similar plugin is to build an archive with the code maven-source-plugin. See the Maven documentation for details.

  • Thank you, Viruch - Andriy Ostashevskiy February