Maven程序集:添加相同工件的不同版本

时间:2010-11-30 10:17:42

标签: java maven-2 maven-plugin maven-assembly-plugin

我使用maven程序集插件创建我的应用程序存档。 我的pom中存在的所有依赖都没有任何问题。

现在我需要包含两个或更多版本的同一个工件。

如果在我的pom中我放

<dependencies>
        [...]
        <dependency>
            <groupId>db.test</groupId>
            <artifactId>my-model</artifactId>
            <version>1.0.3</version>
        </dependency>
        <dependency>
            <groupId>db.test</groupId>
            <artifactId>my-model</artifactId>
            <version>1.1.0</version>
        </dependency>
</dependencies>

来源依赖性解析程序删除旧版本,只有1.1.0打包在存档中

我尝试使用程序集xml描述符文件包含jar。我没有找到任何解决方案。

一种可能的解决方案是手动将所有需要的model.jar放入文件夹中,并告诉程序集将其复制到存档中。但我正在寻找一种更可配置的解决方案。

有什么想法吗?

4 个答案:

答案 0 :(得分:13)

我通过使用maven-dependency-plugin来复制已解决的pom依赖项和其他jar,找到了一个解决方案。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
    <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>false</overWriteSnapshots>
            <overWriteIfNewer>true</overWriteIfNewer>
            <includeScope>runtime</includeScope>
        </configuration>
    </execution>
    <execution>
        <id>copy-model</id>
        <phase>package</phase>
        <goals>
            <goal>copy</goal>
        </goals>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>my.test.pkg</groupId>
                    <artifactId>my-model</artifactId>
                    <classifier>server</classifier>
                    <version>1.0.3</version>
                    <type>jar</type>
                </artifactItem>
                <artifactItem>
                    <groupId>my.test.pkg</groupId>
                    <artifactId>my-model</artifactId>
                    <classifier>server</classifier>
                    <version>1.1.0</version>
                    <type>jar</type>
                </artifactItem>
            </artifactItems>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
    </execution>
</executions>

现在我只需在程序集xml

中添加以下行
    <fileSet>
        <directory>${project.build.directory}/lib</directory>
        <outputDirectory>/lib</outputDirectory>
        <filtered>false</filtered>
        <includes>
            <include>*.jar</include>
        </includes>
        <fileMode>0600</fileMode>
    </fileSet>

答案 1 :(得分:12)

Maven认为一次拥有多个版本的模块没有任何意义。它假定较新的版本替换旧版本。如果不是,那就不是同一个模块。我建议你给新模块一个不同的名称,并确保它有不同的包,以避免选择一个随机模块。

总的来说,Maven试图鼓励良好的应用程序设计,故意让它很难做出已经确定为坏主意的事情。

答案 2 :(得分:1)

另一个丑陋的解决方案可能是使用WAR文件覆盖,利用这个机制在应用覆盖时不关注组件JAR文件的版本这一事实。

答案 3 :(得分:0)

我同意,不同版本意味着更换旧版本。如果我们必须为某些业务需求使用两个不同版本的Web服务。最好在不同的包中生成存根,在添加到maven时,您可以在groupid中指定不同的存根。这应该有用。

相关问题