如何使用maven合并本地jar

时间:2014-08-12 18:12:19

标签: java maven jar

我需要使用maven将一些jar合并到一个普通的jar中。我的系统如下所示

first 
    |_ out 
        |-- jar1.jar
        |-- jar2.jar

second
    |--out
        |-- jar3.jar
        |-- jar4.jar

我想将这4个jar合并到一个jar中并将它们放在

first 
    |-- out
            |-- msg

我尝试使用maven-shade插件,但我不知道如何提供这些jar的路径

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <minimizeJar>true</minimizeJar>
              <filters>
                <filter>
                   <artifact>jar1:jar1</artifact>
                   <includes>
                       <include>**</include>
                   </includes>
                </filter>
                <filter>
                   <artifact>jar2:jar2</artifact>
                   <includes>
                       <include>**</include>
                   </includes>
                </filter>
                <filter>
                   <artifact>jar4:jar4</artifact>
                   <includes>
                       <include>**</include>
                   </includes>
                </filter>
                <filter>
                   <artifact>jar3:jar3</artifact>
                   <includes>
                       <include>**</include>
                   </includes>
                </filter>
              </filters>            
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

如果有任何其他方式,我会对此持开放态度。

1 个答案:

答案 0 :(得分:0)

你不能将jar文件放到另一个jar文件中,-jar文件不能这样工作。您需要的是提取所有jar文件的所有内容,然后再将其压缩到一个jar中。

修改
<include>:指示将哪些依赖关系引入uber JAR

<include>groupId:artifactId</include>

<excludes>:从目标JAR中排除一些不应该存在的内容 - 例如JAR元数据,ant构建文件,文本文件等

类似这样的事情

 <configuration>
    <minimizeJar>true</minimizeJar>
      <artifactSet>
         <includes>
            <include>com.google.guava:guava</include>
            <include>net.sf.trove4j:trove4j</include>
            <include>org.mvel:mvel2</include>
            <include>com.fasterxml.jackson.core:jackson-core</include>
            <include>joda-time:joda-time</include>
          </includes>
        </artifactSet>
        <filters>
           <filter>
              <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/license/**</exclude>
                    <exclude>META-INF/*</exclude>
                    <exclude>META-INF/maven/**</exclude>
                    <exclude>LICENSE</exclude>
                    <exclude>NOTICE</exclude>
                    <exclude>/*.txt</exclude>
                    <exclude>build.properties</exclude>
                </excludes>
            </filter>
        </filters>
 </configuration>