1

我在同一个文件夹中有两个 Maven 项目。一个依赖于另一个,即拥有另一个它的依赖项。现在我想使用 maven shade 插件和launch4j,但对我来说似乎很复杂。

有人可以逐步解释我的情况吗?

所以我有 2 个 POM 文件,每个项目中都有一个。我的多模块文件如下所示:

<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>org</groupId>
  <artifactId>some artifact</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <build>
  <plugins>
      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <encoding>UTF-8</encoding>
              <target>1.5</target>
              <source>1.5</source>
          </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>1.3.1</version>
          <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                      <goal>shade</goal>
                  </goals>
                  <configuration>
                      <transformers>
                          <!-- This bit sets the main class for the executable jar as you otherwise -->
                          <!-- would with the assembly plugin                                       -->
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                              <manifestEntries>
                                  <Main-Class>my_main class from project1</Main-Class>
                              </manifestEntries>
                          </transformer>
                          <!-- This bit merges the various GeoTools META-INF/services files         -->
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                      </transformers>
                  </configuration>
              </execution>
          </executions>
      </plugin>
  </plugins>
</build>
<modules>
  <module>project1</module>
  <module>project2</module>
</modules>
</project>

然后我收到以下错误:在project2中:无法创建阴影工件,项目主工件不存在。

4

3 回答 3

1

我通常同意BrunoJCM,只是想补充一点,您对父 pom 设置的规则仅适用于父 pom,它们不是您想要的“继承”。由于父 pom 不会产生任何 jar,因此您所有的调整都是无用的。

您需要的是始终激活的配置文件。并且配置文件是从父 pom 继承的!

我复制粘贴你的规则,我希望它们能按原样工作:

<profiles>
    <profile>
        <id>run-shade</id>

        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <target>1.5</target>
                        <source>1.5</source>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.3.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <manifestEntries>
                                            <Main-Class>my_main class from project1</Main-Class>
                                        </manifestEntries>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

您还需要自定义每个子模块的配置文件(例如主类)。您可以通过在父 pom 配置文件中放置一个变量来做到这一点:

<Main-Class>${main.class}</Main-Class>

然后在每个模块 pom中定义一个属性:

<properties>
    <main.class>org.company.project.Main</main.class>
</properties>
于 2013-02-13T15:48:29.113 回答
0

尝试将excludes节点添加到您的 shade 配置以排除项目的主要工件。在此处查看类似的问题:Project-Main-Artifact-Does-Not-Exist

<configuration>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <manifestEntries>
                <Main-Class>my_main class from project1</Main-Class>
            </manifestEntries>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
    </transformers>
    <excludes>
        <exclude>${project.groupId}:${project.artifactId}</exclude>
    </excludes>
</configuration>
于 2013-08-24T19:27:17.110 回答
0

您应该在每个模块上配置可执行文件。在父 pom 中(这似乎是您发布的 pom),您无能为力。你应该去你想要一个.exe的每个模块的pom并做我在这个问题上发布的内容:尝试使用Alakai插件将Launch4j集成到Maven项目中

我还回答了关于 .exe 文件的另一个问题,但生成了一个控制台 .exe(没有 gui):Wrapping a java command line application with launch4j 和 maven。这个有一个完整的 pom 并且更新。

父项目和 pom 主要用于聚合相互依赖的模块的构建。所以,在你的情况下,考虑到 project2 依赖于 project1,你应该对 project2 的 pom 做我之前提到的设置它。

于 2012-05-21T19:51:22.760 回答