首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在程序集期间“附加”到Maven依赖项中包含的资源

如何在程序集期间“附加”到Maven依赖项中包含的资源
EN

Stack Overflow用户
提问于 2016-05-23 10:52:44
回答 1查看 874关注 0票数 0

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

代码语言:javascript
复制
<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.stackoverflow.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.stackoverflow.examples.AnalysisEngineTest</mainClass>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <!-- Adapted from <http://stackoverflow.com/a/574650/1391325> -->
                <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.stackoverflow.examples</groupId>
            <artifactId>uimafit-stuff</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

依赖项uimafit-stuffmain/resources/META-INF/types.txt下定义了类型描述文件

代码语言:javascript
复制
classpath*:desc/type/metadata.xml

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

代码语言:javascript
复制
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下有自己的amend-included-resource/src/main/resources/META-INF/types.txt文件

代码语言:javascript
复制
classpath*:desc/types/MyOwnSpecialType.xml

为了使我的包工作,我需要这些文件所表示的类型描述符集合的联合(也就是说,我需要将我的类型描述符路径附加到包含的文件,反之亦然)。当我运行mvn package时,没有依赖项的工件文件只包含“我的”types.txt

代码语言:javascript
复制
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文件,而不是来自我自己项目的JAR:

代码语言:javascript
复制
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的两个版本在某种程度上是“统一的”,以便能够运行组装好的jar而不会出现运行时错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-24 06:46:50

这在构建可执行JAR的uimaFIT文档中得到了解释。

这里是相关章节(在ApacheLicense2.0下)

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37389051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档