score:23

Accepted answer

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

score:1

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.

score:1

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

score:3

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.

score:8

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.


Related Query

More Query from same tag