18
votes

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.

5

5 Answers

22
votes

Build once and share it offline

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.

  1. 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>

  1. Package your project with the goal package assembly:single as shown below

In console,

 mvn package assembly:single

In eclipse,

enter image description here


  1. 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.

enter image description here


  1. You can open the JAR to see the dependencies are packed as shown below.

enter image description here


  1. You can share this JAR to other machines off-line without any more build

8
votes

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.

3
votes

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.

1
votes

If you use maven, a mvn install will install your binary into your ${home}/.m2 folder. Those binary will be available for all other maven job that will run on the same machine.

If you need to share your binary between multiple machine, you can deploy your jars to a repository, like nexus or artifactory.

1
votes

You can just take the jar file of your target folder and copy it to the other machines, otherwise you could use some share repository like nexus oder artifactory to share your built binary. Maven is capable of automatically deploying artifacts into those repositories