Maven插件创建可执行jar,依赖项未解包(带jar的jar)

时间:2013-07-16 11:00:10

标签: java jar dependencies executable

我阅读了很多解决方案来构建带有依赖项的可执行jar(maven shade插件,maven依赖插件,maven程序集插件),所有这些插件解压缩依赖jar并在可执行jar中重新打包它们。打包在可执行jar中解压缩的依赖jar的唯一插件是一个jar插件,但是这个插件在可执行jar中添加了它的转子代码。

有没有像这样创建jar的解决方案:

├─executable.jar
├──lib/
├───dependency1.jar
├───dependency2.jar
.
.
.

和那个解决方案。

2 个答案:

答案 0 :(得分:1)

最常见的方法是使用汇编插件,它允许您以您需要的方式配置包装

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
         <archive>
              <manifest>
                   <mainClass>com.somewhere.Main</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>

您还可以为配置指定程序集描述符

<configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptors>
      <descriptor>src/main/assembly/assembly.xml</descriptor>
    </descriptors>
</configuration>

和assembly.xml本身

<assembly>
    <id>assembly</id>
    <formats>
         <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
             <directory>${project.build.outputDirectory}</directory>
             <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

程序集描述符也可以包含依赖项部分:

<!-- lib -->
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
    </dependencySet>
</dependencySets>

据我所知,你正在寻找最后一个。因为它只是将jar文件包含到程序集中而不进行任何修改。所以最终的解决方案如下:

<assembly>
    <id>assembly</id>
    <formats>
         <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- lib -->
    <dependencySets>
        <dependencySet>
             <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

和pom part:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
             <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
          <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                    <goal>single</goal>
               </goals>
         </execution>
    </executions>
</plugin>

答案 1 :(得分:0)

<plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>