如何在组装期间 "append" 包含在 Maven 依赖项中的资源

How to "append" to a resource included with a Maven dependency during assembly

我正在尝试创建一个 Maven 工件,它应该创建一个可执行包(JAR 格式),并且这个工件依赖于另一个项目,而这个项目本身依赖于工件 org.apache.uima.uimafit-core 2.1.0:

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.Whosebug.examples</groupId>
    <artifactId>amend-included-resource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>${project.artifactId}</name>

    <properties>
        <java.version>1.8</java.version>
        <mainClass>com.Whosebug.examples.AnalysisEngineTest</mainClass>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <!-- Adapted from < -->
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.Whosebug.examples</groupId>
            <artifactId>uimafit-stuff</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

依赖项uimafit-stuffmain/resources/META-INF/types.txt下定义a uimaFIT types.txt type description file:

classpath*:desc/type/metadata.xml

因此,项目结构如下所示:

uimafit-stuff
├── desc
│   ├── type
│   │   ├── metadata.xml
├── META-INF
│   ├── org.apache.uima.fit
│   │   ├── types.txt < "classpath*:desc/type/metadata.xml"

此外,我在 amend-included-resource/src/main/resources/META-INF/types.txt 下有自己的 types.txt 文件:

classpath*:desc/types/MyOwnSpecialType.xml

为了让我的包工作,我需要这些文件中的每一个代表的类型描述符集的联合(即我需要将我的类型描述符路径附加到包含的文件,反之亦然)。当我运行mvn package时,没有依赖的artifact文件只包含"my"types.txt:

amend-included-resource-0.0.1-SNAPSHOT.jar
├── desc
│   ├── types
│   │   ├── MyOwnSpecialType.xml
├── META-INF
│   ├── org.apache.uima.fit
│   │   ├── types.txt < "classpath*:desc/types/MyOwnSpecialType.xml"

但是,在使用描述符 jar-with-dependencies 组装期间创建的 JAR 包含来自 uimafit-stufftypes.txt 文件,而不是来自我自己项目的文件:

amend-included-resource-0.0.1-SNAPSHOT-jar-with-dependencies.jar
├── desc
│   ├── type
│   │   ├── metadata.xml
│   ├── types
│   │   ├── MyOwnSpecialType.xml
├── META-INF
│   ├── org.apache.uima.fit
│   │   ├── types.txt < "classpath*:desc/type/metadata.xml"

然后我如何确保资源 types.txt 的两个版本都以某种方式 "unified" 以便我可以 运行 我组装的 jar 没有 运行 时间错误?

uimaFIT documentation 构建可执行 JAR 中对此进行了解释。

这里是相关部分(在 Apache License 2.0 下)

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.2</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals><goal>shade</goal></goals>
          <configuration>
            <transformers>
              <!-- Set the main class of the executable JAR -->
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>org.apache.uima.fit.example.Main</mainClass>
              </transformer>
              <!-- Merge the uimaFIT configuration files -->
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/org.apache.uima.fit/fsindexes.txt</resource>
              </transformer>
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/org.apache.uima.fit/types.txt</resource>
              </transformer>
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/org.apache.uima.fit/typepriorities.txt</resource>
              </transformer>
              <!-- 
                Prevent huge shaded artifacts from being deployed
                to a Maven repository (remove if not desired) 
              -->
              <outputFile>${project.build.directory}/${artifactId}-${version}-standalone.jar</outputFile>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>