Is it possible to actually build a maven project containing java code to be built once and the binaries can be shared?
Problem: The project I am trying to build would take me about 3-4 hours and requires high internet bandwidth. I am trying to check the possibility of re using this built project among several other machines.
I have worked with c++ projects involving makefiles earlier and this was pretty simpler. I am new to Java / eclipse and would need help to figure out if this is something really possible.
PS: I did try to find existing solutions; they were not straight forward or they say that this can't be done.
In Maven, you can build your project only once and get a JAR file fully packed with all dependencies. So that, you can share this JAR to other machines off-line.
Below are the steps to make it.
- First update your pom.xml with the below setting
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.thanga.MyTest[REPLACE WITH YOUR MAIN CLASS]</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
- Package your project with the goal
package assembly:single
as shown below
In console,
mvn package assembly:single
In eclipse,
- Run this and you can get the two JAR files. One of them
MyFullPack-0.0.1-SNAPSHOT-jar-with-dependencies.jar
has the full dependencies loaded.
- You can open the JAR to see the dependencies are packed as shown below.
- You can share this JAR to other machines off-line without any more build
Is it possible to actually build a maven project containing java code to be built once and the binaries can be shared?
Yes, that's the whole point of Maven, you build the project once, thus generating an artifact (your jar/war ...) which is stored in your local maven repository.
The following command build the project and save it in the local repo :
mvn clean install
However, if you do this, you only have the artifact on your local repo.
A good practise is to create a repository and store your artifacts over there : https://maven.apache.org/repository-management.html
The use of the following command would store the snapshot dependency on the repository :
mvn clean deploy
You can then reuse your components through multiples computer by specifying the dependencies
in your new project's pom.xml file.
You might want to give a look at this guide :
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
You would obviously need to configure the repository and your maven project in order to use a setup of this kind.
First things first. Is your project a web application (war) or an enterprise application (ear) or just a stand alone Jar?
you can use the packaging tag in POM.xml to package your application to a JAR,WAR,EAR
Examples:
<packaging>war</packaging>
<packaging>ear</packaging>
<packaging>jar</packaging>
Then run mvn clean install
In project/src/target you should see the jar,war or ear generated which you can use to deploy on your machine or any other machine.
OR
you can also find that in .m2 folder once you have run install.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With