在循环中执行MavenCli时出错(maven-embedder)?

时间:2014-03-14 16:27:59

标签: java maven maven-embedder

在循环中执行maven命令时有什么问题?目标是更新bundle列表的pom.xml版本。第一次迭代,maven正确执行(更新pom.xml),但它会使所有项目出错。

for (String bundlePath: bundlesToUpdate)
{
    MavenCli cli = new MavenCli();
String[] arguments = {
    "-Dtycho.mode=maven", 
        "org.eclipse.tycho:tycho-versions-plugin:set-version", 
    "-DgenerateBackupPoms=false", 
    "-DnewVersion=" + version};
    int result = cli.doMain(arguments, bundlePath, System.out, System.err);
}

与代码相同的错误:

`MavenCli cli = new MavenCli();
for (String bundlePath: bundlesToUpdate) 
{
    String[] arguments = {
    "-Dtycho.mode=maven", 
    "org.eclipse.tycho:tycho-versions-plugin:set-version", 
    "-DgenerateBackupPoms=false", 
    "-DnewVersion=" + version};
int result = cli.doMain(arguments, bundlePath, System.out, System.err);
}`

第一次,没关系:

[main] INFO org.eclipse.tycho.versions.manipulation.PomManipulator -   pom.xml//project/version: 2.2.6-SNAPSHOT => 2.2.7-SNAPSHOT
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - ------------------------------------------------------------------------
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - Reactor Summary:
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - 
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - XXXX project   ....................... SUCCESS [  0.216 s]
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - com.sungard.valdi.bus.fixbroker.client.bnp ........ SKIPPED
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - XXX project Feature ...................... SKIPPED
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - ------------------------------------------------------------------------
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - BUILD SUCCESS

错误发生后:

[main] ERROR org.apache.maven.cli.MavenCli - Error executing Maven.
[main] ERROR org.apache.maven.cli.MavenCli - java.util.NoSuchElementException
  role: org.apache.maven.eventspy.internal.EventSpyDispatcher
roleHint: 
[main] ERROR org.apache.maven.cli.MavenCli - Caused by: null
[main] ERROR org.apache.maven.cli.MavenCli - Error executing Maven.
[main] ERROR org.apache.maven.cli.MavenCli - java.util.NoSuchElementException
      role: org.apache.maven.eventspy.internal.EventSpyDispatcher
roleHint: 

3 个答案:

答案 0 :(得分:2)

我找到的解决方案是使用Maven Invoker,它可以正常工作:

    public class MavenInvoker {

    public static void main(String[] args) throws IOException, NoHeadException, GitAPIException 
{
    MavenInvoker toto = new MavenInvoker();

    toto.updateVersionMavenInvoker("2.2.8-SNAPSHOT", "TECHNICAL\\WEB" );
}

private InvocationRequest request = new DefaultInvocationRequest();
private DefaultInvoker invoker = new DefaultInvoker();

public InvocationResult updateVersionMavenInvoker(String newVersion, String folderPath)
{
    InvocationResult result = null;
    request.setPomFile( new File(folderPath+"\\pom.xml" ) );

    String version =  "-DnewVersion="+newVersion;
    request.setGoals( Arrays.asList("-Dtycho.mode=maven", 
            "org.eclipse.tycho:tycho-versions-plugin:set-version", 
            "-DgenerateBackupPoms=false", 
            version) );
    try {
        result = invoker.execute( request );
    } catch (MavenInvocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

}

答案 1 :(得分:1)

这适用于我自定义maven插件(使用Maven 3.5.0):

ClassRealm classRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();
MavenCli cli = new MavenCli(classRealm.getWorld());
cli.doMain( ... );

plexus启动器sets the context class loaderClassRealm,可以访问"全球" ClassWorld

不确定该解决方案的稳定性,但到目前为止还不错。

二手进口商品:

import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.apache.maven.cli.MavenCli;

答案 2 :(得分:0)

请参阅电子邮件主题以获取更详细的说明:https://dev.eclipse.org/mhonarc/lists/sisu-users/msg00063.html

似乎正确的方法是在构造上给MainCli一个ClassWorld实例,这样它就可以通过多次调用保持正确的状态。

示例:

final ClassWorld classWorld = new ClassWorld("plexus.core", getClass().getClassLoader());
MavenCli cli = new MavenCli(classWorld);
String[] arguments = {
    "-Dtycho.mode=maven", 
        "org.eclipse.tycho:tycho-versions-plugin:set-version", 
    "-DgenerateBackupPoms=false", 
    "-DnewVersion=" + version};
int result = cli.doMain(arguments, bundlePath, System.out, System.err);