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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4312553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:48:33  来源:igfitidea点击:

Maven assembly : add different version of the same artifact

javamaven-2maven-pluginmaven-assembly-plugin

提问by Vlagorce

I create my application archive with the maven assembly plugin. All the dependency present in my pom are included without any problem.

我使用 Maven 程序集插件创建了我的应用程序存档。我的 pom 中存在的所有依赖项都包含在内,没有任何问题。

Now I need to include two or more version of the same artifact.

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

If in my pom I put

如果在我的 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>

Of source the dependenvcy resolver remove the old version and only the 1.1.0 is packaged in the archive

源代码中的dependenvcy 解析器删除旧版本,只有1.1.0 打包在存档中

I try to include the jar by using assembly xml descriptor file. And I didn't find any solution.

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

A possible solution will be to manually put all the needed model.jar inside a folder and tell the assembly to copy it in the archive. But I'm looking for a more configurable solution.

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

Any idea ?

任何的想法 ?

采纳答案by Vlagorce

I found a solution by using maven-dependency-plugin to copy resolved pom dependencies and additional jar.

我通过使用 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>

Now I just have to add the following lines in my assembly xml

现在我只需要在我的程序集 xml 中添加以下几行

    <fileSet>
        <directory>${project.build.directory}/lib</directory>
        <outputDirectory>/lib</outputDirectory>
        <filtered>false</filtered>
        <includes>
            <include>*.jar</include>
        </includes>
        <fileMode>0600</fileMode>
    </fileSet>

回答by Peter Lawrey

Maven assumes it doesn't make any sense to have more than one version of a module at once. It assumes that a newer version replaces the older version. If it doesn't it is not the same module. I suggest you give the newer module a different name and make sure it has different packages to avoid choising a random module.

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

In general Maven tried to encourage good application design and deliberately makes it difficult to do things it has determined to be a bad idea.

总的来说,Maven 试图鼓励好的应用程序设计,并故意让它很难做它认为是一个坏主意的事情。

回答by Stephen C

Another ugly solution might be to use WAR file overlays, exploiting the fact that this mechanism pays no attention to the versions of component JAR files when applying the overlays.

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

回答by Purushothaman

I agree, different versions means replacing the older one. If we have to consume two different versions of a webservice for some business requirement. It is a good idea to generate the stubs in different packages and while adding to maven you can specify different them in groupid. This should work.

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