Maven Shade插件+ Launch4j

Maven Shade插件+ Launch4j

问题描述:

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

I have two maven projects inside the same folder. One is dependent upon other i.e. has the other one it its dependencies. Now I would like to use maven shade plugin and launch4j but it seems to complicated to me.

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

Can somebody give me a step by step explanation for my case?

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

So I have 2 POM files, one in each project. My multi-module file looks like this:

<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中:无法创建着色的工件,项目主工件不存在.

and then I get the following error: In project2: Failed to create shaded artifact, project main artifact does not exist.

我通常同意 BrunoJCM ,只是想补充一下,您对父pom设置的规则仅适用于父pom,它们不是您想要的继承".由于父pom不会产生任何jar,因此您进行的所有调优都无济于事.

I generally agree with BrunoJCM, just want to add that rules that you have put to parent pom are applied only to parent pom, they are not "inherited" as you intended. As parent pom does not produce any jar, all your tunings are useless.

您需要的是始终处于激活状态的配置文件.配置文件是从父pom继承的!

What you need is a profile which is always activated. And profiles are inherited from parent pom!

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

I copy-paste your rules, I hope they work as is:

<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配置文件中放置一个变量来做到这一点:

You will also want to customize your profile per-submodule (e.g. main class). You can do this by putting a variable in your parent pom profile:

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

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

and then defining a property in per-module pom:

<properties>
    <main.class>org.company.project.Main</main.class>
</properties>