如何制作一个胖子罐? [英] How to create a fat jar?

查看:90
本文介绍了如何制作一个胖子罐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SpringBoot,您具有@SpringBootApplication批注,但是与整洁的Java Spark框架等效吗?

IntelliJ创建了一个Maven项目,我添加了spark依赖项,但是运行安装目标,我得到了一个5 KB的jar,没有清单.不是可执行的jar.

到目前为止创建的pom.xml如下:

 <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>
 

将感谢您的任何建议....谢谢!

解决方案

这里您需要的是一个可执行jar,它不仅包含您的类,还包含所有依赖项中的类. 为此,您可以使用 Maven程序插件.

请参见下面的示例代码.

<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>

然后,运行mvn package,可执行jar应该位于文件夹target中(名称类似于ProjectName-1.0-SNAPSHOT-jar-with-dependencies.jar).

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!

解决方案

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).

这篇关于如何制作一个胖子罐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆