2

鉴于我有以下自定义魔力...

/**
 * @goal portal-test
 */
public class PortalTestMojo extends AbstractMojo {


    /*
     * @parameter
     * @required
     */
    private String baseUrl;

    public void execute() throws MojoExecutionException {
        getLog().info("" + baseUrl.isEmpty());
    }

}

...当我在以下 pom 上使用 mvn test 运行时...

 25   <build>
 26     <plugins>
 27       <plugin>
 28         <groupId>com.lgi.plugins</groupId>
 29         <artifactId>maven-plugin-facade</artifactId>
 30         <version>1.0-SNAPSHOT</version>
 31         <executions>
 32           <execution>
 33             <id>testing-mojo</id>
 34             <phase>test</phase>
 35             <goals>
 36               <goal>portal-test</goal>
 37             </goals>
 38           </execution>
 39         </executions>
 40         <configuration>
 41             <baseUrl>TEST</baseUrl>
 42           
 45         </configuration>
 46       </plugin>
 47     </plugins>
 48   </build>

...我在 baseUrl 上得到一个空指针异常

[ERROR] Failed to execute goal com.lgi.day:maven-plugin-facade:1.0-SNAPSHOT:portal-test (testing-mojo) on project testPom: Execution testing-mojo of goal com.lgi.day:maven-plugin-facade:1.0-SNAPSHOT:portal-test failed. NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.lgi.day:maven-plugin-facade:1.0-SNAPSHOT:portal-test (testing-mojo) on project testPom: Execution testing-mojo of goal com.lgi.day:maven-plugin-facade:1.0-SNAPSHOT:portal-test failed.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:225)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution testing-mojo of goal com.lgi.day:maven-plugin-facade:1.0-SNAPSHOT:portal-test failed.
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:110)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more
Caused by: java.lang.NullPointerException
    at com.lgi.day.PortalTestMojo.execute(PortalTestMojo.java:52)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)

谁能告诉我为什么我的 Maven 配置参数在执行之前没有初始化(当它们在 pom 中明确定义时)??????

上面的 pom 文件用于通过插件的集成测试。这是代码:

import java.io.File;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import com.lgi.day.PortalTestMojo;

public class MojoVaribaleAssignmentTest extends AbstractMojoTestCase {

    private final String TEST = "TEST";

    public void testMojoGoal() throws Exception {
        File testPom = new File(getBasedir(), "src/test/resources/testPom.xml");

        PortalTestMojo mojo = (PortalTestMojo) lookupMojo("portal-test",
                testPom);

        assertEquals(TEST, mojo.getBaseUrl());
        assertEquals(TEST, mojo.getPortalPath());
        assertEquals(TEST, mojo.getUserName());
        assertEquals(TEST, mojo.getPassword());
    }

}

这是插件本身的 pom 文件:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lgi.day</groupId>
  <artifactId>maven-plugin-facade</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-plugin-facade Maven Mojo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.lgi.day</groupId>
        <artifactId>groovy-portal-tests-core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <version>1.0-beta-1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-tools</artifactId>
        <version>RELEASE</version>
    </dependency>
  </dependencies>
</project>

我正在使用 Maven 版本 3.0.3

4

1 回答 1

3

问题解决了!!!

我输入 mvn help:effective pom 并注意到 maven-plugin-plugin 版本是 2.7

然后我查看了生成的插件描述符@target/classes/META-INF/maven/plugin.xml 并注意到参数元素为空()

换句话说,2.7 版的 maven-plugin-plugin 描述符目标不会解释出现在注释中的旧的遗留注释。

我升级了我的 pom 以使用更新版本的 maven-plugin-plugin 并将 maven-plugin-annotations 添加为依赖项。此处概述了必要的更改:

http://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html

然后我注释了魔力...

@Mojo(name = "portal-test")
public class PortalTestMojo extends AbstractMojo {

    @Parameter(required = true)
    private String baseUrl;

...ETC

然后将参数元素填充到插件描述符中

于 2012-11-27T12:05:12.830 回答