将单个jar文件中的不同java类捆绑在一个jar中

时间:2010-01-26 23:43:03

标签: java maven-2 jar dependencies

基本上我所拥有的是运行liferay门户的应用服务器。 我们还有自定义portlet,它们共享自定义库以及第三方库。 例如dwr-3.0,几个drools jar文件,spring-webmvc,spring等。

问题在于服务器管理是一场噩梦,因为当有部署时,总会忘记服务器中的版本等。

我们使用maven 2,我想的是做一些类似maven项目的东西,它从父pom中提取依赖项(使用dependencyManagement),默认目标就像是“解开所有依赖项并将它们放入jar中”独特的罐子“。 这样我们就会拥有一个带有标准版本的独特jar,它与其他文件一起部署而不必部署。

你们知道如果可能的话我该怎么做? 我一直在使用maven-assembly-plugin而没有太大的成功。如果装配是要走的路,你有一个例子给我一个kickstart吗?

提前致谢

5 个答案:

答案 0 :(得分:2)

另一种选择是maven-shade-plugin甚至是jarjar。我已经使用了这两个,以及包重命名没有任何问题。

答案 1 :(得分:1)

听起来好像Dependency Plugin可能就是你所需要的。

我想你可以将所有依赖jar解包到指定的位置,然后将它们重新打包到你的新jar中。

我希望它适合你。

答案 2 :(得分:1)

我认为你所描述的是可能的。首先,创建一个父POM,在{P}中声明依赖关系:

<dependencyManagement>

然后,在子项目中,声明所需的依赖项而不声明其版本:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>groupIdA<groupId>
  <artifactId>parent</artifactId>
  <packaging>pom<packaging>
  <version>1-SNAPSHOT</version>
  ...
  <dependencyManagement>
    <!-- Standard dependencies used in several build modules. Ensures all modules
         use the same version for these dependencies -->
    <dependencies>
      <dependency>
        <groupId>org.directwebremoting</groupId>
        <artifactId>dwr</artifactId>
        <version>3.0.M1</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.0.RELEASE</version>
      </dependency>
      ...
    </dependencies>
  <dependencyManagement>
  ...
</project>

最后,使用默认的jar-with-dependencies预定义程序集描述符来创建二进制包的通用程序集,其中所有依赖库都包含在存档中解压缩。

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>parent</artifactId>
    <groupId>groupIdA</groupId>
    <version>1-SNAPSHOT</version>
  </parent>
  <artifactId>childB</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <dependencies>
    <dependency>
      <groupId>org.directwebremoting</groupId>
      <artifactId>dwr</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
    </dependency>
      ...
  </dependencies>
  ...
</project>

要创建项目程序集,请触发阶段:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-5</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- append to the packaging phase. -->
            <goals>
              <goal>single</goal> <!-- goals == mojos -->
            </goals>
          </execution>
        </executions>
      </plugin>
      [...]
    </plugins>
  </build>
</project>

这将在目标目录中生成以下程序集:

mvn package

我只是不确定你想用这个程序集做什么(在portlet项目中使用它作为依赖关系来从父POM中拉出依赖关系?仅缓解liferay部署?)。但是所有选项都是可能的。

有关详细信息,请参阅Maven Assembly plugin文档。

答案 3 :(得分:1)

我终于能够通过以下方式解决它,我还没有在应用程序服务器中进行测试,但生成的存档是一个包含所有所需依赖项内容的jar文件。 Thanks Vincent for the tip :)

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.jarbundle</groupId>
<artifactId>bundle</artifactId>
<version>1.0</version>
<name>bundle</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>org.directwebremoting</groupId>
        <artifactId>dwr</artifactId>
        <version>3.0.M1</version>
        <scope>runtime</scope>
        <exclusions>
         <exclusion>
          <artifactId>cometd</artifactId>
          <groupId>org.mortbay.jetty</groupId>
         </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5</version>
        <scope>runtime</scope>
        <exclusions>
         <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
         </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>2.5</version>
        <scope>runtime</scope>
        <exclusions>
         <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
         </exclusion>
         <exclusion>
          <artifactId>aopalliance</artifactId>
          <groupId>aopalliance</groupId>
         </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>2.5</version>
        <scope>runtime</scope>
        <exclusions>
         <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
         </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>4.0.3</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>4.0.3</version>
        <scope>runtime</scope>
        <exclusions>
         <exclusion>
          <artifactId>antlr-runtime</artifactId>
          <groupId>org.antlr</groupId>
         </exclusion>
         <exclusion>
          <artifactId>core</artifactId>
          <groupId>org.eclipse.jdt</groupId>
         </exclusion>
         <exclusion>
          <artifactId>janino</artifactId>
          <groupId>janino</groupId>
         </exclusion>
         <exclusion>
          <artifactId>xercesImpl</artifactId>
          <groupId>xerces</groupId>
         </exclusion>
         <exclusion>
          <artifactId>xml-apis</artifactId>
          <groupId>xml-apis</groupId>
         </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-jsr94</artifactId>
        <version>4.0.3</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-decisiontables</artifactId>
        <version>4.0.3</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>jar.App</mainClass>
                    </manifest>
                </archive>
            </configuration> 
        </plugin>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>${basedir}/target/dependency</directory>
        </resource>
    </resources>
</build>

然后你运行

mvn package

link text

答案 4 :(得分:0)

最后,最好的解决方案是使用maven-shade-plugin:

(snip)
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <finalName>${artifactId}-${version}-tmp</finalName>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>full</shadedClassifierName>
                        <artifactSet>
                            <excludes>
                                <exclude>classworlds:classworlds</exclude>
                                <exclude>junit:junit</exclude>
                                <exclude>jmock:*</exclude>
                                <exclude>org.apache.maven:lib:tests</exclude>
                                <exclude>log4j:log4j:jar:</exclude>
                            </excludes>
                        </artifactSet>
                        <filters>
                            <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>
    </plugins>
</build>

(剪断)

什么是关于shade插件很棒的是当你包含重叠的类时它会发出警告(一个例子是带有aopalliance类的spring jar(完整的))