带有maven shade插件的父子pom布局

Parent and child pom layout with maven shade plugin

我正在尝试制作一个包含所有项目的超大罐子。 如果我这样做 "mvn package",我会在 "blah" 项目目标文件夹下得到一个 uber jar。 (blah 项目有主要 class。) uber jar 包含所有项目(作为文件夹而不是 jars),但是当我 运行 它时,它似乎无法识别 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 为 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 的 classes/functions。

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

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

如果您使用的是 serviceloader,这就是我所做的。

feature1/src/main/resources/META-INF/services/"some super class" feature2/src/main/resources/META-INF/services/"some super class"

如果您使用文本编辑器打开这两个文件,每个文件都有一个子 class 名称供您使用 serviceloader。我将它们复制并附加到 jar 中的 "some super class" 文件中。