Peter-TI Peter-TI - 1 month ago 19
Java Question

Maven add dependecies to project

In eclipse you can easily add dependecies (i.e. a JAR file) to you're project.
Right click on the project and click -> Build Path-> add libraries.

Now a hidden file is createn inside the project, ".classpath".
Inside this file is a classpathEntry added so now I can use the libraries by adding it in a java file:

import foo.bar.*;


This "application" can now be exported to a single jar.

How can I achieve this with Maven and without eclipse?
I switched to emacs... :)

With the command:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-cli -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


I get myself a sample start application.
inside a src folder is a App.java file writing "hello World" on a commandline.

with the following in the pom.xml I obtain myself a jar inside my project(maven indedependent deployment) succesfully:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mycompany.third-party</groupId>
<artifactId>commons-cli-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>false</overWrite>
<!--${project.basedir} ${project.build.directory} -->
<outputDirectory>${project.basedir}/resources/repo</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.basedir}/resources/repo</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>


Now how can I use this jar inside my App.java?
Do I have to create a .classpath file manually?
How can I arrange my classpathentries automatically?
I have been trying to create a manifest.MF with the classpassentries but with no succes. I have tried several tutorials.

I had no succes with:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>${project.basedir}
/resources/repo/optional- new-name.jar</include>
</includes>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true
</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<mainClass>your.main.Class</mainClass>



</manifest>
<manifestEntries>
<Class-Path>${project.basedir}/resources/repo</Class-Path>

</manifestEntries>

<manifestFile>
<!--
${project.build.outputDirectory}/META-INF/MANIFEST.MF

-->
${project.basedir}/META-INF/MANIFEST.MF
</manifestFile>

</archive>

<!--
<archive>
<manifest>
<mainClass>com.mkyong.core.App</mainClass>
</manifest>
</archive>
-->


</configuration>




after creating the manifest.MF manually.

after the command

mvn clean install


the manifest.MF remains empty.

Help would be appreciated.

Thanks!

Answer

+1 for not using eclipse ;^)

If you are looking to reference code from another JAR then try adding the following to your POM:

<dependencies>
    <dependency>
        <groupId>xxx</groupId>
        <artifactId>yyy</artifactId>
        <version>zzz</version>
    </dependency>
</dependencies>

Where xxx, yyy and zzz are the maven coordinates for the JAR you want to import. You can get these from your Nexus

If you are looking to combine the contents of all your dependencies with your code and release them as a single JAR then you might want to take a look at the maven-shade-plugin. See How to package a jar and all dependencies within a new jar with maven

And one other thing, you should never play with the .classpath file. This is something Eclipse specific. Changing it won't effect Maven and will probably confuse Eclipse

Comments