从多个项目创建JAR文件,相互依赖

时间:2017-05-04 13:59:44

标签: java maven jar executable-jar

我有三个apache-camel项目,比方说project-Aproject-Bproject-C。 project-A有java main方法,它依赖于project-B而project-B依赖于project-C(project-C的依赖项在project-B中添加,project-B依赖项在project-A中添加)。我想为project-B创建jar文件。我试图通过在project-A的pom.xml -

中放置以下配置来创建jar文件
<plugins>
     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <exclude>org.jboss.seam:jboss-seam</exclude>
                            </excludes>
                        </artifactSet>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>com.getusroi.featureinstaller.FeatureMetaInfoConfigInstaller</Main-Class>
                                </manifestEntries>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/services/org/apache/camel/TypeConverter</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />

                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/maven/plugin.xml</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/spring.tooling</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/info.xml</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/DISCLAIMER</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/README.txt</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/DEPENDENCIES</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/LICENSE</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/LICENSE.txt</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/NOTICE</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/NOTICE.txt</resource>
                            </transformer>
                        </transformers>
                        <filters>
                            filter out jar signatures from shaded jars
                            <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>
        <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</plugins>

我面临的问题:在项目-B和项目-C中,我有两个同名的文件,因此在创建jar文件后,一个文件被另一个文件覆盖。任何帮助将非常感激。

1 个答案:

答案 0 :(得分:1)

让父项目同时构建所有3个项目:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.greg</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>example</name>

  <modules>
    <module>project-a</module>
    <module>project-b</module>
    <module>project-c</module>
  </modules>

</project>

每个项目都可以根据需要具有本地项目依赖关系,project-b pom.xml将如下所示:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>project-b</artifactId>

  <dependencies>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>project-c</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</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>com.greg.Application</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

将创建一个在project-b / target-1.0-SNAPSHOT.jar中运行main方法com.greg.Application的executbale jar