how to create source.jar on maven install - java

I'm working in Sprint Tool Suite and I've created a maven project. When I right click on the project I can select Run as...Maven Install. This takes all my class files and packages them into a jar file as expected and also creates a copy to my central maven repository C:\Users\Owner.m2\repository
All that is fine, but I'd like it to also package up all the source files into a sources.jar file. I suspect I'll need to edit the pom.xml file for the project to make that happen, but haven't seen an example of how to do that. Please advise.

You have to add proper plugin to your execution cycle
<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>

Related

Handle different source folder in Eclipse

The Project I am working on has the regular src/main and src/test folders, but they recently introduced src/integration-test, which is supposed to contain, as the name suggests, integration-tests.
Unfortunately I can't seem to make this work with Maven and Eclipse. I tried adding a source folder, but Eclipse is not able to resolve the dependencies needed for the integration-tests and fails to compile the test-classes. I also can't start the tests with JUnit.
The source folder was added to the build path, but it did not help.
All other developers in my team use IntelliJ and report that they don't have these issues at all. But it´s bound to work with Eclipse as well, right?
You should handle this with maven by adding the source folder to the build:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
In this example the build will add a src/integration folder where you can put other test classes.
For more information check the build helper plugin documentation.

Eclipse not recognizing generated class

I have used Maven plugin to generate classes in my project. However even after trying padding it explicitly from project build path, eclipse is unable to recognize it. It says,
"AbcBaseListener cannot be resolved to a type"
Project auto build is also ON.
Check 2 things:
1)
The important hint here is "class folder". Eclipse expects .class files in there, source code will be ignored.
To fix this, add this plugin to your POM:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
and update the project (Maven -> Update ...).
The other option would be to manually add another source folder to the build path but that will get lost whenever m2e updates the project configuration from the POM.
2) Make sure your generated files are having package names;
Use below code inside your g4 file after grammar Abc;
#header {
package antlr4;
}

How to generate a war file using relative link source folder

I have created a maven project in eclipse and took the java classes from another folder. I have chosen the create links in work space option instead of import. But while generating the war file maven is not including the java files. How to build a project with source links in maven?
Look at this question:
How to properly include Java sources in Maven?
This is helpful information in above post:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

Class not able to recognize maven dependencies

I am trying to create a maven project. I added the dependencies as jar files and importing them in my class. But the class doesn't recognize it.
I tried a couple of options and serached but couldn't resolve. Please guide.
Attached below is the screenshot of my project str
Try this:
Right click your project and select maven then click update project. Select the project and select force update of snapshots/releases option. Then refresh the project and build it (if you do not see compilation error after updating).
Since the jar is not under web-inf folder you are seeing this error.
There are several ways to resolve this issue
add below profile
<profile>
<id>qa</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Dependencies directory created in target folder
mvn install dependency:copy-dependencies

Retrieve dependencies source code in maven project

In my maven project I have plenty of dependencies which source code I need to get.
I know there is maven-dependency plugin with unpack-dependencies goal in it
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>resolve-dependecies</id>
<phase>process-sources</phase>
<goals>
<goal>sources</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<includeParents>true</includeParents>
<type>java-source</type>
</configuration>
</execution>
<execution>
<id>src-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.basedir}/Utils/src</outputDirectory>
<type>java-source</type>
</configuration>
</execution>
</executions>
</plugin>
But when I use it a lot files "have NOT been resolved".
e.g.
...
org.jboss:jboss-parent:java-source:sources:5
org.apache.cxf:cxf-parent:java-source:sources:3.0.3
org.apache:apache:java-source:sources:13
org.apache:apache:java-source:sources:9
org.liquibase:liquibase-parent:java-source:sources:3.5.0
org.apache.commons:commons-parent:java-source:sources:5
org.eclipse.jetty.orbit:jetty-orbit:java-source:sources:1
...
In pom.xml I didn't specify any repository for dependencies, so by default maven central repository is using.
I also tried to decompile (Fernflower, Procyon, JAD projects) my project jar file which contains all dependencies. But after decompiling hundreds of errors appeared in result java files.
I still hope to solve this issue using maven tools.
Thanks in advance for any help.
EDIT
Unresolved artifacts directly not specified in pom.xml
I see a couple of dependencies with *-parent. Given the name I guess these are merely parent poms which probably don't contain any java sources. For the remaining ones, are you sure that there are artifacts with the java sources?
UPDATE: I checked the list you shown within your question whether there are sources and there weren't. What I noted is that all of them have packaging POM. You might skip these by including <includeClassifiers>sources</includeClassifier> within your configuration

Categories