This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Question: How do I use Maven to create executable jars with dependencies?

I want to package my project into an executable JAR for distribution.

How do I make the Maven project package all the dependency jars into my output JARS?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}


<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>
Copy the code

And then use

mvn clean compile assembly:single
Copy the code

The compile target should be added before Assembly: Single: single, otherwise the code in your own project is not included.

Check out the comments for more details.

Typically, this goal is related to the automated construction phase. This ensures that the JAR is built when MVN install or deploy/publish is performed.

<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <! -- this is used for inheritance merges --> <phase>package</phase> <! -- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>Copy the code

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 5…

As a Java programmer, spring Boot is still a must!!

Purpose: Package the specified environment and package a JAR with dependencies.

<build> <finalName>eureka</finalName> <defaultGoal>compile</defaultGoal> <resources> <resource> <directory>src/main/resources/conf</directory> <filtering>true</filtering> <excludes> <exclude>dev/*</exclude> <exclude>test/*</exclude> <exclude>pro/*</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>${start-class}</mainClass> <! -- <layout>ZIP</layout> <excludes> <exclude> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclude> <exclude> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclude> </excludes> --> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>compile</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>true</overwrite> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <resources> <resource> <directory>src/main/resources/conf/${package.environment}</directory> <filtering>true</filtering> </resource> <resource>  <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>static/*.*</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build>Copy the code

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️