我正在开发一个maven插件,它可以在项目中运行mvn compile。
我尝试将此代码添加到扩展AbstractMojo (类似于https://github.com/rzymek/watcher-maven-plugin)的类中:
@Component
private Maven maven;
...
maven.execute(request);它会抱怨maven为null。于是我试着:
Maven maven = new DefaultMaven();
maven.execute(request);但这会在org.apache.maven.DefaultMaven.execute(DefaultMaven.java:170)产生一个NullPointerException,如这个未回答的问题Programmatic builds using Maven 3 API中所述
我也试过了
ClassRealm containerRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();
ContainerConfiguration cc = new DefaultContainerConfiguration().setName( "maven" ).setRealm( containerRealm ).setClassPathScanning( PlexusConstants.SCANNING_INDEX ).setAutoWiring( true );
DefaultPlexusContainer container = new DefaultPlexusContainer( cc );
Maven maven = (Maven) container.lookup( "org.apache.maven.Maven", "default" );
maven.execute(request);但这抛出了一个NoSuchElementException:
org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException
role: org.apache.maven.Maven
roleHint: default我尝试使用getPluginContext()中的角色提示,也得到了相同的结果。
发布于 2014-04-05 15:38:42
你需要告诉maven生成插件描述文件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<!-- see http://jira.codehaus.org/browse/MNG-5346 -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>
</build>https://stackoverflow.com/questions/22870804
复制相似问题