使用Maven Shade插件创建可执行JAR

时间:2012-06-09 21:20:50

标签: maven maven-3 maven-assembly-plugin maven-shade-plugin

我在使用简单的Java程序制作可执行jar时遇到问题。

我关注了these Maven instructions并修改了我的pom.xml

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation= 
           "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>package.MyClass</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

执行install目标时,我可以阅读以下内容:

[INFO] Replacing /home/project-1.0.0.jar with /home/project-1.0.0-shaded.jar

但是我在那个位置找到的只是project-1.0.0.jar,实际上包含了依赖关系 - 并且具有正确的大小 - 但是当我试图用我的jre执行它时,它说:

The file '/home/project-1.0.0.jar' is not marked as executable.

因此没有任何东西可以执行!

如何让我的jar正确执行?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

  

给出 pom.xml 来创建可执行jar文件

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.executablejar</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
    <java-version>1.8</java-version>
    <docker.image.prefix>springDemo</docker.image.prefix>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>demo</finalName>
</build>

答案 1 :(得分:-1)

我有一个使用程序集插件的简单解决方案。这个解决方案将创建一个jar(target / $ {project.groupdId} - $ {project.artifactId} - $ {project.version} .jar):

  • 包含所有必需的依赖项
  • 可执行(通过java -jar * .jar)

在你的pom文件中声明:

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>

            <configuration>
                <appendAssemblyId>false</appendAssemblyId>

                <archive>
                    <manifest>
                        <mainClass>package.MyClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>

            <executions>
                <execution>
                    <id>make-executable-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我希望我能帮到你! 干杯!