只在Maven中创建可执行的jar-with-dependencies

时间:2011-02-16 12:34:38

标签: maven jar

在我的pom.xml中,我使用maven-assembly-plugin在运行“mvn clean install”时创建一个可执行的jar-with-dependencies。 现在它首先创建不可执行的“name-version.jar”,然后创建“name-version-jar-with-dependencies.jar”。

我可以以某种方式配置pom.xml,以便它不会创建不可执行的JAR文件吗?

目前我使用< appendAssemblyId> false< / appendAssemblyId>所以它只是覆盖了第一个文件......

另外,我得到了几个“...已添加,跳过”消息。我可以以某种方式阻止他们吗?

这是我pom.xml中的maven-assembly-plugin定义:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <mainClass>my.main.class</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>

6 个答案:

答案 0 :(得分:7)

我使用maven-jar-plugin和maven-assembly-plugin的单一目标来实现这个

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <finalName>finalName</finalName>
                <archive>
                    <manifest>
                        <mainClass>
                            mainClass
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

无论如何,最重要的是告诉maven不要使用maven-jar-plugin的配置生成默认jar

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>default-jar</id>
            <phase>none</phase>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:4)

我想我已经想出了如何做到这一点。你必须离开jar,否则你将失去编译器和资源复制,以及与构建你需要的jar相关的所有其他东西。

我的解决方案基本上涉及欺骗maven,所以我想要注意的是,在未来的版本中,这很可能无法正常工作......

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>dumpster-diver</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>DumpsterDiver</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>

请注意,在列出的构建配置中,maven-assembly-plugin和构建本身的finalName匹配。另请注意<appendAssemblyId>false</appendAssemblyId>。如果这样做,构建和程序集jar的输出文件名将完全匹配。 Maven肯定会警告你,但是会把你想要的jar放在你的目标目录和你的本地存储库中。

以下是此项目中'mvn clean install'的输出,以及目标目录和本地存储库中jar的jar -tvf:

$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building dumpster-diver
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory <currentDirectory>/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to <currentDirectory>/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory <currentDirectory>/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar
[INFO] [assembly:single {execution: default}]
[INFO] Processing DependencySet (output=)
[WARNING] Artifact: <groupId>:dumpster-diver:jar:0.1.0-SNAPSHOT references the same file as the assembly destination file. Moving it to a temporary location for inclusion.
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: <currentDirectory>/target/dumpster-diver.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: <currentDirectory>/target/archive-tmp/dumpster-diver.jar
with assembly file: <currentDirectory>/target/dumpster-diver.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing <currentDirectory>/target/dumpster-diver.jar to /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Aug 18 13:08:56 EDT 2011
[INFO] Final Memory: 22M/618M
[INFO] ------------------------------------------------------------------------
krohr@krohr-Latitude-E6500:<currentDirectory>
$ jar -tvf ./target/dumpster-diver.jar | grep -i "dump"
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
     0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/
  2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml
       126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties
krohr@krohr-Latitude-E6500:<currentDirectory>
$ jar -tvf /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar | grep -i "dump"
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
     0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/
  2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml
   126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties

答案 2 :(得分:1)

您可以尝试设置为pom。这将阻止创建default 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/xsd/maven-4.0.0.xsd">
  ...
  <packaging>pom</packaging>
  ...
</project>

答案 3 :(得分:0)

也许我对你的问题遗漏了一些东西但是下面的代码让我成了一个可执行的jar:这就是你要找的东西吗?

              <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>2.3.1</version>
              <configuration>
                    <archive>
                          <manifest>
                              <addClasspath>true</addClasspath>
                                <mainClass>some.main.class</mainClass>
                          </manifest>
                    </archive>
                    <finalName>Proj-${project.version}</finalName>
                    <outputDirectory>target</outputDirectory>
              </configuration>
        </plugin>

答案 4 :(得分:0)

您可以在下面使用此版本并使用以下命令进行编译:

mvn clean编译程序集:单个

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>youMainClass</mainClass>
                        </manifest>
                    </archive>

                    <finalName>${artifactId}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
        </plugin>
    </plugins>
</build>

答案 5 :(得分:0)

如果有帮助,我可以按以下方式解决此问题,它消除了所有警告,并在与原始输出jar相同的位置替换了uber jar,更像是spring boot uber jar插件在没有spring的情况下所做的事情启动依赖项。

关键方面:

  • appendAssemblyId = true
  • attach = false
  • antrun重命名文件
  • 插件执行顺序由POM文件中的顺序定义
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <attach>false</attach>
        <archive>
          <manifest>
            <mainClass>foo.bar.Baz</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <id>rename-jar-with-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <move file="${project.build.directory}/${project.build.finalName}.jar"
            tofile="${project.build.directory}/${project.build.finalName}.jar.original" />
          <move file="${project.build.directory}/${project.build.finalName}-jar-with-dependencies.jar"
            tofile="${project.build.directory}/${project.build.finalName}.jar" />
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>