代码之家  ›  专栏  ›  技术社区  ›  Carl Smotricz

自定义maven程序集

  •  8
  • Carl Smotricz  · 技术社区  · 15 年前

    我想创建一个程序集

    1. jar-with-dependencies 类似于这个名称的“标准”目标,但不包括一些资源。我想要 log4j.properties 以及其他一些配置文件

    2. 建立一个 .ZIP .jar 以及上述配置文件。

    assembly:assembly assembly:single .

    • 我需要自定义程序集描述符吗?
    • pom.xml ? 所以它进去了 src/assembly/something.xml 并被一个 descriptorRef ?
    • 我可以将其编码为两个相对简单的程序集,其中一个是在另一个程序集上构建的(即.Zip程序集使用.Jar程序集),还是必须在一个程序集中执行所有操作?
    1 回复  |  直到 15 年前
        1
  •  28
  •   Pascal Thivent    15 年前

    欢迎登机,卡尔!:D个

    我想从命令行(仅)启动这个程序集,因此不需要绑定到某个阶段(或目标)?魔咒?)。更倾向于使用装配:装配或组件:单。

    只是想澄清一下: build lifecycle phases (编译、测试、打包等)和插件目标(从技术上讲是mojo)是分阶段的。然后调用一个阶段。。。或者只是一个特定的插件目标。

    我需要自定义程序集描述符吗?

    既然你想要 pre-defined descriptors 别掩护,是的。你甚至需要两个(当然是uberjar,一个是zip)。

    我真的不能把它藏在pom.xml?所以在src/assembly中/something.xml文件并被一个描述符ref引用?

    src/main/assembly . 没有, descriptorRef 对于内置描述符,您必须使用 descriptor 在这里。

    我可以将其编码为两个相对简单的程序集,其中一个是在另一个程序集上构建的(即.Zip程序集使用.Jar程序集),还是必须在一个程序集中执行所有操作?

    正如所暗示的,您将需要两个程序集描述符。我来帮你。。。

    假设您有以下项目结构:

    $ tree .
    .
    ├── pom.xml
    └── src
        ├── main
        │   ├── assembly
        │   │   ├── jar.xml
        │   │   └── zip.xml
        │   ├── java
        │   │   └── com
        │   │       └── stackoverflow
        │   │           └── App.java
        │   └── resources
        │       └── log4j.properties
        └── test
            └── java
                └── com
                    └── stackoverflow
                        └── AppTest.java
    

    在哪里 pom.xml 包含程序集插件的以下配置:

    <project>
      ...
      <dependencies>
        ...
      </dependencies>
      ...
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
              <descriptors>
                <descriptor>src/main/assembly/jar.xml</descriptor>
                <descriptor>src/main/assembly/zip.xml</descriptor>
              </descriptors>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    “已筛选”uberjar的描述符(jar.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>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <directory>${project.build.outputDirectory}</directory>
          <outputDirectory>/</outputDirectory>
          <excludes>
            <exclude>log4j.properties</exclude>
          </excludes>
        </fileSet>
      </fileSets>
    </assembly>
    

    这个描述符的作用是(简而言之):

    • 但是 排除项目本身(是的,这是违反直觉的,但是为了向后兼容,保留了这种奇怪的默认行为)
    • 包括项目文件,但排除其中一些文件。

    以及zip的描述符(zip.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>bin</id>
      <formats>
        <format>zip</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <fileSets>
        <fileSet>
          <directory>${project.basedir}/src/main/resources</directory>
          <outputDirectory/>
          <includes>
            <include>log4j.properties</include>
          </includes>
        </fileSet>
        <fileSet>
          <directory>${project.build.directory}</directory>
          <outputDirectory/>
          <includes>
            <include>*-uberjar.jar</include>
          </includes>
        </fileSet>
      </fileSets>
    </assembly>
    

    这是(不知怎么的)自我解释:)

    • 它包括配置文件(相对于 <directory> )在装配的根部
    • 它包括uberjar(相对于 <目录>

    最后,快跑 mvn assembly:assembly (这是打算在CLI上使用的目标)。


    我没有(有意地)将META-INF/maven/**包含在uberjar的程序集中。有没有一个简单的方法来防止这些被包括进来?

    unpackOptions . 下面是 jar.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>
          <unpackOptions>
            <excludes>
              <exclude>META-INF/maven/**</exclude>
            </excludes>
          </unpackOptions>
          <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <directory>${project.build.outputDirectory}</directory>
          <outputDirectory>/</outputDirectory>
          <excludes>
            <exclude>log4j.properties</exclude>
          </excludes>
        </fileSet>
      </fileSets>
    </assembly>