【问题标题】:Parent and child pom layout with maven shade plugin带有 maven shade 插件的父子 pom 布局
【发布时间】:2019-02-03 13:12:50
【问题描述】:

我正在尝试制作一个包含所有项目的胖 uber jar。 如果我执行“mvn package”,我会在“blah”项目 taget 文件夹下获得一个 uber jar。 (blah 项目有主类。) uber jar 包含所有项目(作为文件夹而不是 jar),但是当我运行它时,它似乎无法识别 feature1 和 feature2 项目。

父 pom:

<plugins>

  <!-- download source code in Eclipse, best practice -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>false</downloadJavadocs>
    </configuration>
  </plugin>

  <!-- Set a compiler level -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>

  <!-- Maven Shade Plugin -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
      <!-- Run shade goal on package phase -->
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <!-- add Main-Class to manifest file -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.a.blah.main</mainClass>
            </transformer>
          </transformers>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>

</plugins>
<modules>
  <module>blahtask</module>
  <module>feature1</module>
  <module>feature2</module>
  <module>blahmessaging</module>
  <module>blah</module>
</modules>

pom for blah

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.39</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>com.a.blah</groupId>
  <artifactId>blahtask</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.a.blah</groupId>
  <artifactId>blahmessaging</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.a.fs</groupId>
  <artifactId>feature1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.a.fs</groupId>
  <artifactId>feature2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>

我为上面的 feature1 和 feature2 添加了依赖项,以便它们在 uber jar 文件中。这是错的吗? p.s. blahmessaging、feature1 和 feature2 使用来自 blahtask 的类/函数。

很难找到包含多个项目的 maven-shade-plugin 示例。很难找到他们的 poms 文件应该如何以及父子文件的结构。

【问题讨论】:

  • 如果有人制作了一个包含多个项目并且运行良好的jar,请提供示例。谢谢...
  • 顺便说一句:删除 maven-eclipse-plugin 因为它已经退役了很长时间......

标签: java maven jar pom.xml maven-shade-plugin


【解决方案1】:

原来是服务加载器问题... 我在 feature1 和 feature2 项目中手动添加了用于 serviceloader 的类名。

如果您使用的是服务加载器,这就是我所做的。

feature1/src/main/resources/META-INF/services/“一些超类” feature2/src/main/resources/META-INF/services/“一些超类”

如果您使用文本编辑器打开这两个,则每个服务加载器都需要一个子类名称。我将它们复制并附加到 jar 中的“一些超类”文件中。

【讨论】:

    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2012-03-17
    • 2021-07-12
    • 2011-12-10
    • 2014-07-23
    相关资源
    最近更新 更多