简体   繁体   English

Maven项目取决于同一工件的两个版本

[英]Maven project depending on two versions of the same artifact

I have a project with two seperate modules that uses sqlline and another library(say OtherLib) that depends on jline. 我有一个项目有两个单独的模块,使用sqlline和另一个依赖于jline的库(比如OtherLib)。 However on different versions. 但是在不同的版本上。

External Libraries 外部图书馆

Module1 uses Sqlline depends on jline 2.10 Module1使用Sqlline依赖于jline 2.10
Module2 uses OtherLib depends on jline 0.9.94 Module2使用OtherLib依赖于jline 0.9.94

And the two versions are incompatible. 这两个版本是不兼容的。 Therefore I have set the classpaths such that Module1 will search in $HOME/lib/module1 folder first and Module2 in $HOME/lib folder first. 所以我已经设置了类路径,使得模块1将在$ HOME / lib目录/文件夹模块1首先第一搜索和单词数在$ HOME / lib文件夹。

Is there a way to specify maven to download the artifact to a source directory first(say ../resources/lib ) and then copy it to package during packaging time in assembly.xml ? 有没有办法指定maven首先将工件下载到源目录(例如../resources/lib ),然后在assembly.xml打包时间将其复制到包中?

I know that for copying from source directory the following code can be used. 我知道,对于从源目录复制,可以使用以下代码。

<fileSets>
   <fileSet> 
        <directory>../resources/lib</directory>
        <outputDirectory>${HOME}/lib/module1</outputDirectory>
        <directoryMode>755</directoryMode>
        <fileMode>644</fileMode>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
</fileSets>

Also to get maven to download the dependency I can use (for Module2) 还要让maven下载我可以使用的依赖项(对于Module2)

 <dependencySets>
    <dependencySet>
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>${HOME}/lib</outputDirectory>
        <directoryMode>755</directoryMode>
        <fileMode>644</fileMode>
        <includes>
            <include>jline:jline:jar:0.9.94</include>
        </includes>
    </dependencySet>
<dependencySets>

How can I make sure jline:jline:jar:2.10 is downloaded to ../resources/lib folder first? 如何确保jline:jline:jar:2.10首先下载到../resources/lib文件夹?

If you're absolutely sure, what you're doing, you can repackage one of the version using something like maven-shade-plugin . 如果你完全确定,你正在做什么,你可以使用maven-shade-plugin类的东西重新打包其中一个版本。 But please be absolutely sure, what you're doing. 但请绝对肯定,你正在做什么。

With maven-shade-plugin you could create a new Maven module, say jline:jline_2_10:jar:1.0 and use jline:jline:jar:2.10 as a dependency. 使用maven-shade-plugin您可以创建一个新的Maven模块,例如jline:jline_2_10:jar:1.0并使用jline:jline:jar:2.10作为依赖项。 The maven-shade-plugin would then package it in your jline_2_10-1.0.jar . maven-shade-plugin然后将它打包在你的jline_2_10-1.0.jar

Since your new artifact has its own groupId:artifactId combination, there will be no conflicts with the other jline:jline:jar:0.9.94 artifact, so you'll happily have both in the classpath. 由于你的新工件有自己的groupId:artifactId组合,因此不会与其他jline:jline:jar:0.9.94冲突jline:jline:jar:0.9.94工件,所以你很乐意在类路径中同时使用它们。

I found a an answer here using maven-dependency-plugin 我在这里找到了一个使用maven-dependency-plugin的答案

In pom.xml pom.xml中

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.1</version>
         <executions>
           <execution>
               <id>copy-model</id>
               <phase>package</phase>
               <goals>
                  <goal>copy</goal>
               </goals>
               <configuration>
                 <artifactItems>
                   <artifactItem>
                       <groupId>jline</groupId>
                       <artifactId>jline</artifactId>
                       <version>2.10</version>
                       <type>jar</type>
                   </artifactItem>
                 <artifactItems>
                 <outputDirectory>../../resources/lib</outputDirectory>
               </configuration>
           </execution>
        </executions>
     </plugin>
   <plugins>  
 <build>

And in assembly.xml 并在assembly.xml中

   <fileSet>
        <directory>../../resources/lib</directory>
        <outputDirectory>${HOME}/lib/module1</outputDirectory>
        <directoryMode>755</directoryMode>
        <fileMode>644</fileMode>
        <includes>
            <include>jline-*</include>
        </includes>
    </fileSet>

jline-0.9.94 is included in a dependencySet as any other dependency. jline-0.9.94被包括在dependencySet任何其他依赖关系。 I hope this helps. 我希望这有帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM