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

Java 在循环中执行MavenCli时出错(maven embedder)?,java,maven,maven-embedder,Java,Maven,Maven Embedder,在循环中执行maven命令时出现了什么问题?目标是更新捆绑包列表的pom.xml版本。在第一次迭代中,maven execute正确地更新pom.xml,但它会在更新后对所有项产生错误 for (String bundlePath: bundlesToUpdate) { MavenCli cli = new MavenCli(); String[] arguments = { "-Dtycho.mode=maven", "org.eclipse.tycho:ty

在循环中执行maven命令时出现了什么问题?目标是更新捆绑包列表的pom.xml版本。在第一次迭代中,maven execute正确地更新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: 

我找到的解决方案是使用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;
}

}

我找到的解决方案是使用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;
}

}

有关详细说明,请参阅电子邮件线程:

似乎正确的方法是在构造时为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);

有关更多详细说明,请参阅电子邮件线程:

似乎正确的方法是在构造时为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);

这在使用maven 3.5.0的自定义maven插件中适用:

ClassRealm classRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();
MavenCli cli = new MavenCli(classRealm.getWorld());
cli.doMain( ... );
plexus启动器可以访问其类领域,该领域可以访问全局类世界

不确定这个解决方案有多稳定,但到目前为止看起来不错

二手进口:

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

这在使用maven 3.5.0的自定义maven插件中适用:

ClassRealm classRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();
MavenCli cli = new MavenCli(classRealm.getWorld());
cli.doMain( ... );
plexus启动器可以访问其类领域,该领域可以访问全局类世界

不确定这个解决方案有多稳定,但到目前为止看起来不错

二手进口:

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

我在下面的答案中找到了解决方案。我在下面的答案中找到了解决方案。这在maven插件中对我不起作用。但它引导我走向正确的方向,看我的答案。谢谢这在maven插件中对我不起作用。但它引导我走向正确的方向,看我的答案。谢谢这是有用的,但我发现了我必须做的困难的方法:final ClassRealm ClassRealm=ClassRealm Thread.currentThread.getContextClassLoader;最终ClassWorld=classRealm.getWorld;classWorld.disposeRealmmaven.ext;最终MavenCli cli=新的MavenCliclassWorld;很遗憾,官方文档太少,几乎没有用处,包括Javadocs!,就像很多Apache一样,Maven似乎是最糟糕的部分之一,但至少我可以看看Github上的Maven插件源代码……我得出了一个痛苦的结论,MavenCli在插件中不可用,日志被弄乱了,所以IDEA认为它已经提前停止了!我看过ApacheMaven Invoker库,Maven Invoker插件使用它,它为Maven EXE构建了一个CLI字符串并运行它!这显然是对自己的图书馆缺乏信心!!!我感觉到你了。我认为解决方案可能不会长期稳定。不幸的是,我不知道更多,没有在较新的Maven版本中再次使用该代码。它提供了classcast例外这是有用的,但我发现了我必须做的困难的方法:final ClassRealm ClassRealm=ClassRealm Thread.currentThread.getContextClassLoader;最终ClassWorld=classRealm.getWorld;classWorld.disposeRealmmaven.ext;最终MavenCli cli=新的MavenCliclassWorld;很遗憾,官方文档太少,几乎没有用处,包括Javadocs!,就像很多Apache一样,Maven似乎是最糟糕的部分之一,但至少我可以看看Github上的Maven插件源代码……我得出了一个痛苦的结论,MavenCli在插件中不可用,日志被弄乱了,所以IDEA认为它已经提前停止了!我看过ApacheMaven Invoker库,Maven Invoker插件使用它,它为Maven EXE构建了一个CLI字符串并运行它!这显然是对自己的图书馆缺乏信心!!!我感觉到你了。我认为解决方案可能不会长期稳定。不幸的是,我不知道更多,没有在更新的Maven版本中再次使用该代码。