带外部罐子的Maven组装

时间:2017-03-27 18:14:14

标签: java maven jar maven-assembly-plugin

我正在尝试创建一个程序集(一个可运行的jar,就是这样),并希望所有的EXTERNAL jar都以“jar”格式包含在我的程序集中,而不是从外部jar中取出的类和成为我的“runnable”jar文件的一部分。 我已尝试在程序集文件中使用“unpack”选项(将其设置为false),但它会产生其他问题,例如,它不会为我的项目生成CLASS文件,更不用说包括处理外部jar文件了。所以我肯定做错了什么。 非常感谢帮助。 POM和ASSEMBLY信息如下。 感谢

以下是我的POM文件的摘录。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>src/assembly/assemblyinfo.xml</descriptor>
        </descriptors>
        <archive>
            <manifest>
                <mainClass>com.company.sample</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>        

这是assemblyinfo.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0                   http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>FULL</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

1 个答案:

答案 0 :(得分:0)

<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.evermore.talos.services.ttservice.ttstarter.UIStartup</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>compile</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.shared</groupId>
                        <artifactId>maven-filtering</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/release</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <filtering>true</filtering>
                                </resource>
                                <resource>
                                    <directory>target</directory>
                                    <includes>
                                        <include>*-dependencies.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

请参阅此Maven构建设置。这就是我们如何构建具有所有依赖关系的可执行jar。