How to create a fat jar?

风流意气都作罢 提交于 2021-02-18 07:57:50

问题


With SpringBoot, you have the @SpringBootApplication annotation, but what is the equivalent with the neat Java Spark framework?

IntelliJ creates a Maven project and I added the spark dependency, but running the install goal, I get a 5 KB jar with no manifest. Not an executable jar.

The pom.xml created thus far is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>sparkdemo</groupId>
  <artifactId>sparkdemo</artifactId>
  <version>1.0 </version>
  <packaging>jar</packaging>

  <name>sparkdemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.sparkjava</groupId>
      <artifactId>spark-core</artifactId>
      <version>2.5.4</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <optimize>true</optimize>
          <debug>true</debug>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

Would appreciate any suggestions....Thanks!


回答1:


What you need here is an executable jar that will not only contain your classes, but also classes from all your dependencies. For that you can use the Maven Assembly Plugin.

See the sample code below.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>     // specify your main class here
            </manifest>
          </archive>
        </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>
      [...]
</project>

Then, run mvn package and the executable jar should be located in the folder target (with a name like ProjectName-1.0-SNAPSHOT-jar-with-dependencies.jar).




回答2:


Looks like you are looking for maven assembly plugin

Add this in the plugin section and run mvn package

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>


来源:https://stackoverflow.com/questions/41490310/how-to-create-a-fat-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!