Intellij Java 2016 & Maven : how to embed dependencies in JAR?

This question already has an answer here:

  • How can I create an executable JAR with dependencies using Maven? 33 answers

  • I finally managed to generate this JAR with Intellij Java, here is how I do:

  • add the dependencies in the pom.xml file
  • go to File -> Project Structure -> Artifacts -> New -> JAR -> From module with dependencies
  • choose the Main class and click OK
  • in your project, in src/main, create the "resources" folder
  • move the "META-INF" (with MANIFEST.MF in it) folder in this "resources" folder
  • go to Build -> build artifacts to build the JAR
  • EDIT

    A better (and easier way) to do it is adding the following lines in the pom.xml file :

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>your.MainClass</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    then use the "clean" and "package" maven commands.

    The last 3 steps above (about MANIFEST.MF) still seem to be mandatory.


    Okay, so you basically want to create a "fat jar" (sometimes called assembly), that contains all its own dependencies (usually, the dependencies are external).

    You need to use a Maven plugin for that. Below is a sample assembly plugin configuration jar-with-dependencies:

    <project>
      ...
      <build>
        ...
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
            ...
    </project>
    

    then, simply run

    mvn package
    
    链接地址: http://www.djcxy.com/p/29622.html

    上一篇: 如何将maven依赖关系包含在jar文件中?

    下一篇: Intellij Java 2016和Maven:如何在JAR中嵌入依赖关系?