Maven-assembly-plugin Java engineering into an executable jar package __maven

Source: Internet
Author: User

Wrote a small tool in Java, using MAVEN Java engineering. When you are finished, you want to package into an executable jar package.
Use Maven's plugin maven-assembly-plugin

Add in Pom.xml

<build>
      <plugins>
        <plugin>
          <artifactid>maven-assembly-plugin</ artifactid>
          <configuration>
            <archive>
              <manifest>
                <mainClass> org.kuan.wang.app</mainclass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </ configuration>
        </plugin>
      </plugins>
    </build>

To execute a command:

MVN Clean Compile Assembly:single

Generate Maventestapp-1.0-snapshot-jar-with-dependencies.jar in target folder
This jar pack is the executable jar, and can be executed with the command Java-jar Path\jarfile.
Note: Compile must be assembly:single before your project code (class file) will not be packaged .

Assembly:single is the goal of Maven.
Typically, this goal is bound to the phase of the MAVEN build so that it can be executed automatically. MVN package is used to package jar packages. Just the jar package is not executable.
In the above Pom file Riga <executions>

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>org.kuan.wang.App</mainClass>
      </ manifest>
    </archive>
    <descriptorRefs>
      <descriptorref>jar-with-dependencies </descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!--this are used for inheritance merges-->
      < Phase>package</phase> <!--bind to the packaging phase-->
      <goals>
        <goal>single </goal>
      </goals>
    </execution>
  </executions>
</plugin>

In this case, run the following command directly.

MVN Clean Package

There are also a few points to note.
1. It is necessary to have

<archive>
     <manifest>
          <mainClass>org.kuan.wang.App</mainClass>
     </ Manifest>
</archive>

Or you'll get an error.
No main manifest attribute, in Target\maventestapp-1.0-snapshot-jar-with-dependencies.jar
<mainClass> is the entrance to your own project.
2. Using mvn clean compile assembly:single, only one jar package is generated, with jar-with-dependencies in name, which can be executed.
Using MVN package to generate two jar packages, the Jar-with-dependencies jar in the name is executable.
3. If you use <goal>assembly</goal>
MVN assembly:assembly with a command generates two jar packages. Same as using the MVN package.
There is no need to add compile to the front like command assembly:single.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.