【发布时间】:2021-01-05 21:07:00
【问题描述】:
我正在尝试使用 maven plugin testing harness 测试我的 maven 插件。我能找到的关于此事的唯一文档相当陈旧,我发现 similar 线程有相同的错误,但没有解决方案,至少没有一个为我解决问题的解决方案。该错误可以归结为尝试运行 lookupMojo 方法时抛出的 NoSuchElementException。
是否有其他人遇到过此问题或类似问题,您是如何解决的?如果您需要更多信息,请告诉我,我会发布更新。
插件类
@Mojo(name = "my_plugin", defaultPhase = LifecyclePhase.CLEAN, threadSafe = true)
public class MyPlugin extends AbstractMojo
{
private static final Logger logger = LoggerFactory.getLogger(MyPlugin.class);
@Parameter private String configFileLocation;
public void execute() throws MojoExecutionException, MojoFailureException
{
logger.info("The config file location is: {}", configFileLocation);
saveSystemProperties(new File(configFileLocation));
}
private void saveSystemProperties(final File file)
{
logger.info("Attempting to save system properties");
try(FileOutputStream fr = new FileOutputStream(file))
{
System.getProperties().store(fr, "Properties");
logger.info("Properties successfully saved. Closing File Output Stream Implicitly");
}
catch(IOException e)
{
logger.info("There was an IO error. ");
e.printStackTrace();
}
}
}
插件测试类
public class MyPluginTester extends AbstractMojoTestCase
{
private static final Logger logger = LoggerFactory.getLogger(MyPluginTester.class);
protected void setup() throws Exception
{
super.setUp();
}
protected void tearDown() throws Exception
{
super.tearDown();
}
public void testMojoGoal() throws Exception
{
logger.info("Loading Test Pom File");
File testPom = new File(getBasedir(),"src/test/resources/pom/basic-test-plugin-config.xml");
assertNotNull(testPom);
MyPlugin mojo = (LittleSysStore)lookupMojo("configure", testPom);
assertNotNull(mojo);
}
}
POM 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.stuff</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<properties>
<maven.plugin.annotations.version>3.5</maven.plugin.annotations.version>
<maven.plugin.testing.version>3.3.0</maven.plugin.testing.version>
<maven.version>3.5.0</maven.version>
<slf4j.version>1.7.25</slf4j.version>
<junit.version>4.12</junit.version>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${maven.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven.plugin.annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>${maven.plugin.testing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
输出
org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException
role: org.apache.maven.plugin.Mojo
roleHint: com.my.stuff-maven-plugin:1.0-SNAPSHOT:clean
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:267)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:243)
at org.codehaus.plexus.PlexusTestCase.lookup(PlexusTestCase.java:205)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.lookupMojo(AbstractMojoTestCase.java:410)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.lookupMojo(AbstractMojoTestCase.java:355)
at unit_tests.LittleSysStoreTest.testMojoGoal(LittleSysStoreTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.util.NoSuchElementException
at java.util.Collections$EmptyIterator.next(Collections.java:4189)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:263)
... 23 more
【问题讨论】:
-
您的 MyPluginTester 是如何知道该方法的? AbstractMojoTestCase 类是否包含名为lookupMojo 的方法?也许该方法是私有的而不是公共的?
-
正确。 AbstractMojoTestCase 是一个包含在 org.apache.maven.plugin.testing 包中的类。它应该包含执行测试所需的所有内容,或者根据链接的文档进行接缝。
-
确实如此,从我收集到的搜索此问题的信息来看,这是一个常见问题,没有记录在案的解决方案
-
尝试用字符串调用它,字符串参数只是为了看看库是否知道其他可用的方法声明?
-
立即抛出未找到文件异常。让我修改路径,我会回复你。
标签: java maven maven-plugin