在准备命令期间覆盖 Maven 依赖项解析的默认值

Posted

技术标签:

【中文标题】在准备命令期间覆盖 Maven 依赖项解析的默认值【英文标题】:Override defaults for Maven's dependancy resolution during prepare command 【发布时间】:2016-03-16 21:37:48 【问题描述】:

我希望能够在不需要用户输入的情况下运行 maven prepare。 目前,我必须手动输入“yes”和“0”才能正确解析每个项目的依赖关系。



更新:由于'no'和'1'是默认值,我不能使用'-B'在批处理模式下运行它。



Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T16:58:10-04:00)
Maven home: C:\apps\apache-maven\bin\..
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: C:\Program Files (x86)\Java\jdk1.6.0_45\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

插件:maven-release-plugin目标:准备

手动输入示例:

还有一些剩余的快照依赖项。 : 你想现在解决吗? (是/否) 否: : 是 要解析的依赖类型,:指定选择编号( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ):(0/1/2/3) 1: : 0 依赖 'gov.xxxx.abc.api:Abc2_Common_Model' 是一个快照 (8.0.0.8-SNAPSHOT) : 应该设置为哪个发行版本? 8.0.0.8: : 应该将依赖项重置为哪个版本以进行开发? 8.0.0.8: :

完整示例:

[INFO] ------------------------------------------------------------------------
[INFO] Building Abc2_Master 8.0.0.8-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-release-plugin:2.3.2:clean (default-cli) @ abc-2-root ---
[INFO] Cleaning up after release...
[INFO]
[INFO] --- maven-release-plugin:2.3.2:prepare (default-cli) @ abc-2-root ---
[INFO] Verifying that there are no local modifications...
[INFO]   ignoring changes on: **\release.properties, **\pom.xml.next, **\pom.xml.releaseBackup, **\pom.xml.backup, **\pom.xml.branch, **\pom.xml.tag
[INFO] Executing: cmd.exe /X /C "svn --non-interactive status"
[INFO] Working directory: C:\Users\joshua.nell\Documents\00-CBA-Main\workspaces\clean_checkout\CreateJar\R8.0\PROD_Release_8_0
[INFO] Checking dependencies and plugins for snapshots ...
There are still some remaining snapshot dependencies.
: Do you want to resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: : 0
Dependency 'gov.xxxx.abc.api:Abc2_Common_Model' is a snapshot (8.0.0.8-SNAPSHOT)
: Which release version should it be set to? 8.0.0.8: :
What version should the dependency be reset to for development? 8.0.0.8: :
There are still some remaining snapshot dependencies.
: Do you want to resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: : 0
Dependency 'gov.xxxx.abc.api:Abc2_Common_Model' is a snapshot (8.0.0.8-SNAPSHOT)
: Which release version should it be set to? 8.0.0.8: :
What version should the dependency be reset to for development? 8.0.0.8: :
There are still some remaining snapshot dependencies.
: Do you want to resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: : 0
Dependency 'gov.xxxx.abc.api:Abc2_Common_Model' is a snapshot (8.0.0.8-SNAPSHOT)
: Which release version should it be set to? 8.0.0.8: :
What version should the dependency be reset to for development? 8.0.0.8: :
There are still some remaining snapshot dependencies.
: Do you want to resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: : 0
Dependency 'gov.xxxx.abc.api:Abc2_Common_Model' is a snapshot (8.0.0.8-SNAPSHOT)
: Which release version should it be set to? 8.0.0.8: :
What version should the dependency be reset to for development? 8.0.0.8: :
What is the release version for "Abc2_Master"? (com.xxxx.abc2:abc-2-root) 8.0.0.8: :
....

更新:

由于人们似乎被困在快照上,我还使用versions-maven-plugin 来处理将其设置为发布版本并递增。

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>versions-maven-plugin</artifactId>
    <version>$maven-version-plugin.ver</version>
    <configuration>
      <includes>
        <include>gov.xxxx.abc.api:Pts2_Common_Model</include>
      </includes>
      <allowMinorUpdates>true</allowMinorUpdates>
    </configuration>
  </plugin>

更新: 作为参考,pom.xml 中有一个主要的奇怪之处(当前版本不允许更改),这可能是导致需要手动输入的奇怪之处。

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>$maven-jar-plugin.ver</version>
    <executions>
      <execution>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <includes>
            <include>gov/xxxx/abc/api/common/models/**</include>
            <include>gov/xxxx/abc/api/common/cache/**</include>
            <include>gov/xxxx/abc/api/intra/models/**</include>
            <include>gov/xxxx/abc/api/models/**</include>
            <include>gov/xxxx/abc/api/webtools/**</include>
            <include>gov/xxxx/abc/api/common/Utils*</include>
            <include>gov/xxxx/abc/api/common/PTSConstants*</include>
            <include>gov/xxxx/abc/api/sms/Sms*</include>
            <include>gov/xxxx/abc/api/sms/dao/DebugLoggingStoredProc*</include>
            <include>gov/xxxx/abc/api/sms/util/**</include>
            <include>gov/xxxx/abc/api/sms/outbound/**</include>
            <include>gov/xxxx/abc/api/sms/dao/models/**</include>
            <include>com/xxxx/ent/esb/ems/sms/**</include>
            <include>com/xxxx/ent/esb/ems/sms/outbound/**</include>
            <include>com/xxxx/ent/esb/ems/sms/services/**</include>
            <include>com/xxxx/accessmgt/ems/sms/**</include>
            <include>com/xxxx/ent/esb/repository/**</include>
            <include>gov/xxxx/abc/api/monitor/**</include>
            <include>gov/xxxx/abc/api/monitor/dao/**</include>
            <include>gov/xxxx/abc/api/statistics/util/**</include>
          </includes>
          <finalName>Pts2_Common_Model-$project.version</finalName>
        </configuration>
      </execution>
    </executions>
  </plugin>

【问题讨论】:

因此,您正在寻求进行非交互式发布,但带有 hacky 偏见。这里有人问过类似的问题:***.com/q/8449579/4807777 看看它的答案给出的一些风险。否则,带有目标版本的版本 Maven 插件可能会有所帮助。 这与发布版本无关。这些不需要更改默认值。这是关于需要不同默认值的Do you want to resolve them now? (yes/no)specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 输入。 我从未对此进行过测试,但您可以使用 release.properties: maven.apache.org/maven-release/maven-release-plugin/examples/… 配置发布插件。我不确定那里是否有存储selection number 之类的字段,我稍后会测试,但这可能是要走的路。 【参考方案1】:

'no' 和 '1' 默认是硬编码的

这里是 maven-release-manager "CheckDependencySnapshotsPhase" 的代码

private void resolveSnapshots( Set<Artifact> projectDependencies,Set<Artifact> reportDependencies, Set<Artifact> extensionDependencies, Set<Artifact> pluginDependencies, ReleaseDescriptor releaseDescriptor ) throws ReleaseExecutionException

    try
    
        prompter.showMessage( RESOLVE_SNAPSHOT_MESSAGE );
        String result =
            prompter.prompt( RESOLVE_SNAPSHOT_PROMPT, Arrays.asList( "yes", "no" ), "no" );

        if ( result.toLowerCase( Locale.ENGLISH ).startsWith( "y" ) )
        
            Map<String, Map<String, String>> resolvedSnapshots = null;
            prompter.showMessage( RESOLVE_SNAPSHOT_TYPE_MESSAGE );
            result = prompter.prompt( RESOLVE_SNAPSHOT_TYPE_PROMPT,
                                      Arrays.asList( "0", "1", "2", "3" ), "1" );

要覆盖这些值,您必须更改代码如下:

Arrays.asList( "yes", "no" ), "no" ); --> Arrays.asList( "yes", "no" ), "yes" );

Arrays.asList( "0", "1", "2", "3" ), "1" ); --> Arrays.asList( "0", "1", "2", "3" ), "0" );

svn: http://svn.apache.org/repos/asf/maven/release/tags/maven-release-2.5.3/maven-release-manager

【讨论】:

使用versions-maven-plugin。但正如我所说,这与快照版本无关。版本控制工作正常。我试图覆盖 BEFORE 它询问有关版本控制的问题。 那么你是说解决这个问题的唯一方法是创建我自己的那个 maven 插件的自定义版本? 你可以看到默认答案的字符串是“硬编码”的,所以我看不到任何其他方式。

以上是关于在准备命令期间覆盖 Maven 依赖项解析的默认值的主要内容,如果未能解决你的问题,请参考以下文章

Maven:如何覆盖库添加的依赖项

使用子解析器时,命令行参数被默认值覆盖

多模块项目构建期间模块之间的 Maven 依赖关系解析

gradle 到 maven 的转换,在依赖项的依赖项的版本名称中解析通配符“+”?

InstallShield - 在升级期间防止修补程序覆盖注册表值?

Maven:在不扩展依赖项的情况下使用 flatten 解析版本