【问题标题】:How to use maven to build an self-contained executable jar? [duplicate]如何使用maven构建一个自包含的可执行jar? [复制]
【发布时间】:2017-08-08 20:05:15
【问题描述】:

我正在使用 maven 构建一个 java 项目。我的项目有很多依赖项。现在,我需要在服务器上部署这个项目,所以我想创建一个自包含的可执行 jar,其中包含所有依赖项。我遇到了一个老问题,建议使用 maven shade 插件,但我想知道是否可以仅使用 maven 来做到这一点。 有没有一种方法可以只使用 maven 创建一个自包含的可执行 jar?

【问题讨论】:

    标签: java maven


    【解决方案1】:

    我认为你需要的是肥罐或单罐。将以下内容添加到您的 pom 文件并更改 属性

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
    
            <!-- Skip the tests run. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.15</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
    
            <!-- Sources generation -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>**********Put main class here********</mainClass>
                        </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>
        </plugins>
    </build>
    

    像往常一样使用mvn包来构建jar。

    【讨论】:

      【解决方案2】:

      这里有一个链接,解释了如何使用 maven 构建可执行 JAR:

      https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

      【讨论】:

      • 这也是在使用阴影插件。有没有办法只用 maven 来做到这一点?
      • Maven 使用插件,shade 允许你创建一个可执行的 JAR。尝试使用阴影,如果遇到问题,请发布
      • 好的。我会尝试阴影插件。
      【解决方案3】:

      其他人已经回答了,但这里是 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>com.greg</groupId>
          <artifactId>blackbox-tester</artifactId>
          <version>1.0-SNAPSHOT</version>
          <packaging>jar</packaging>
      
          <name>blackbox-tester</name>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>org.apache.httpcomponents</groupId>
                  <artifactId>httpclient</artifactId>
                  <version>4.5.3</version>
              </dependency>
              <dependency>
                  <groupId>org.apache.httpcomponents</groupId>
                  <artifactId>httpcore</artifactId>
                  <version>4.4.6</version>
              </dependency>
              <dependency>
                  <groupId>org.apache.commons</groupId>
                  <artifactId>commons-io</artifactId>
                  <version>1.3.2</version>
              </dependency>
              <dependency>
                  <groupId>org.apache.commons</groupId>
                  <artifactId>commons-lang3</artifactId>
                  <version>3.5</version>
              </dependency>
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <configuration>
                          <source>1.8</source>
                          <target>1.8</target>
                      </configuration>
                  </plugin>
      
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-shade-plugin</artifactId>
                      <version>3.0.0</version>
                      <executions>
                          <execution>
                              <phase>package</phase>
                              <goals>
                                  <goal>shade</goal>
                              </goals>
                              <configuration>
                                  <transformers>
                                      <transformer
                                              implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                          <manifestEntries>
                                              <Main-Class>com.greg.blackboxtester.Application</Main-Class>
                                          </manifestEntries>
                                      </transformer>
                                  </transformers>
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
      
              </plugins>
          </build>
      
      </project>
      

      您现在可以使用以下命令运行 jar:

      java -jar 目标/blackbox-tester-1.0-SNAPSHOT.jar

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-22
        • 2010-12-21
        • 2017-03-19
        • 2018-01-31
        • 2011-04-12
        • 2019-05-22
        相关资源
        最近更新 更多