Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to build a java project only once using eclipse and share?

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.

like image 585
Siva Avatar asked Feb 05 '16 05:02

Siva


3 Answers

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

like image 155
Thanga Avatar answered Oct 22 '22 03:10

Thanga


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.

like image 8
Hello_world Avatar answered Oct 22 '22 02:10

Hello_world


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.

like image 3
Karthik Arun Avatar answered Oct 22 '22 03:10

Karthik Arun