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

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


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.


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.