问题
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?
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>
and then I get the following error: In project2: Failed to create shaded artifact, project main artifact does not exist.
回答1:
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.
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>
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>
and then defining a property in per-module pom:
<properties>
<main.class>org.company.project.Main</main.class>
</properties>
回答2:
You should configure the executable on each module. In the parent pom (which seems what the pom you posted is) there's is nothing you can do. You should go the pom of each module you want a .exe and do what I posted on this question: Trying to integrate Launch4j in a Maven project using Alakai plugin
I also answered this other question about .exe files, but generating a console .exe (without gui): Wrapping a java command line application with launch4j and maven. This one have a complete pom and is newer.
Parent projects and poms are used mainly to aggregate the build of modules that depends on each other. So, in your case, considering that project2 depends on project1, You should do to the project2's pom and setup it as I mentioned before.
回答3:
Try adding an excludes node to your shade configuration to exclude the project's main artifact. See the similar problem here: 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>
来源:https://stackoverflow.com/questions/9285386/maven-shade-plugin-launch4j