'Packaging jar file with source code using Maven so breakpoints can be used in Eclipse
Requirements
I am refactoring a Java application and am trying to move code out of a war file and into a jar file so it can be used across many different web applications. However, one of the requirements is that the developers still need to use breakpoints within the library. To do so I believe I need to package the source with the compiled code. Below is the build lifecycle
Build Lifecycle
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Question
When I run mvn package
and mvn source:jar package
it creates two jar files, respectively
- db-0.0.1-SNAPSHOT.jar
- db-0.0.1-SNAPSHOT-sources.jar
Then I can attach the source to the dependency.
My question is two-fold
- Is there anyway to package the source code into the same jar file as the compiled classes, and
- Would this auto-attach the source code to the dependency in Eclipse, so that the developers don't need to do it automatically?
Solution 1:[1]
Putting the source code into the JAR is the wrong approach.
You create two JARs (as shown in your question). These JARs go to the repository. Then everyone who uses the JARS will automatically get the -sources
as well. In the usual IDEs, like Eclipse and IntelliJ the -sources
JAR is automatically used for debugging.
Instead of two Maven runs, you should add the source plugin to your POM. Then the -sources
JAR is created during the normal build.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | J Fabian Meier |