代码之家  ›  专栏  ›  技术社区  ›  Starkey

复杂的maven装配

  •  1
  • Starkey  · 技术社区  · 14 年前

    我正在写一个Maven程序集,我不知道如何继续。这是相当复杂的,所以我谷歌的例子真的没有帮助。这就是我要做的:

    • 使用launch4j创建安装文件。假设JAR文件已正确组装(因此需要maven程序集),此部分工作正常。
    • 安装程序包含一些依赖项。这些是用 jar-with-dependencies descriptorRef . 这也行。
    • 我需要将一个战争文件(来自另一个项目)包含到大jar中。这是我的困惑。

    如何创建 assembly.xml 这将同时完成JAR和依赖项(解包所有这些JAR文件),并包括来自另一个项目(未解包)的WAR文件。

    任何帮助都将不胜感激。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Pascal Thivent    14 年前

    如何创建一个assembly.xml,它将同时执行JAR和依赖项(解包所有这些JAR文件),并包含来自另一个项目(未解包)的WAR文件。

    假设您有一个类似于下面的项目结构(我假设一个简单的结构,因为您没有提到它的任何特别之处):

    .
    ├── pom.xml
    └── src
        ├── main
        │   ├── assembly
        │   │   └── uberjar.xml
        │   └── java
        │       └── com
        │           └── stackoverflow
        │               └── App.java
        └── test
            └── java
                └── com
                    └── stackoverflow
                        └── AppTest.java
    

    使用以下pom.xml:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.stackoverflow</groupId>
      <artifactId>Q3762049</artifactId>
      <version>1.0-SNAPSHOT</version>
      <dependencies>
        <!-- this is the war we want to include in the assembly -->
        <dependency>
          <groupId>com.mycompany</groupId>
          <artifactId>my-webapp</artifactId>
          <version>1.0-SNAPSHOT</version>
          <type>war</type>
          <scope>runtime</scope>
        </dependency>
        <!-- and below, the other dependencies -->
        <dependency>
          <groupId>commons-lang</groupId>
          <artifactId>commons-lang</artifactId>
          <version>2.4</version>
        </dependency>
        ...
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
              <descriptors>
                <descriptor>src/main/assembly/uberjar.xml</descriptor>
              </descriptors>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    正如你所看到的,

    1. 我们不会使用 jar-with-dependencies 这里的描述符,我们将在自己的自定义程序集描述符中重用它。
    2. 我们对战争的依赖 runtime 范围,以便我们能够将其包含在程序集中。

    现在,习惯 uberjar.xml :

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
      <id>uberjar</id>
      <formats>
        <format>jar</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <dependencySets>
        <dependencySet>
          <unpack>true</unpack>
          <scope>runtime</scope>
          <useProjectArtifact>false</useProjectArtifact>
          <excludes>
            <exclude>*:war</exclude>
          </excludes>
        </dependencySet>
        <dependencySet>
          <unpack>false</unpack>
          <scope>runtime</scope>
          <useProjectArtifact>false</useProjectArtifact>
          <includes>
            <include>*:war</include>
          </includes>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <directory>${project.build.outputDirectory}</directory>
          <outputDirectory>/</outputDirectory>
        </fileSet>
      </fileSets>
    </assembly>
    

    这是 具有依赖关系的JAR 将创建JAR的描述符:

    • 除了战争以外的依赖,没有包装
    • webapp之战,未开箱
    • 项目本身的类

    如下所示:

    $ mvn clean package
    [INFO] Scanning for projects...
    ...
    $ cd target; jar xvf Q3762049-1.0-SNAPSHOT-uberjar.jar
      created: META-INF/
     inflated: META-INF/MANIFEST.MF
      created: org/
      created: org/apache/
      created: org/apache/commons/
      created: org/apache/commons/lang/
      created: org/apache/commons/lang/builder/
      created: org/apache/commons/lang/enum/
      created: org/apache/commons/lang/enums/
      created: org/apache/commons/lang/exception/
      created: org/apache/commons/lang/math/
      created: org/apache/commons/lang/mutable/
      created: org/apache/commons/lang/text/
      created: org/apache/commons/lang/time/
     inflated: META-INF/LICENSE.txt
     inflated: META-INF/NOTICE.txt
     inflated: org/apache/commons/lang/ArrayUtils.class
    ...
      created: META-INF/maven/
      created: META-INF/maven/commons-lang/
      created: META-INF/maven/commons-lang/commons-lang/
     inflated: META-INF/maven/commons-lang/commons-lang/pom.xml
     inflated: META-INF/maven/commons-lang/commons-lang/pom.properties
     inflated: my-webapp-1.0-SNAPSHOT.war
      created: com/
      created: com/stackoverflow/
     inflated: com/stackoverflow/App.class