在自定义maven插件中执行之前未初始化mojo配置

时间:2012-11-26 17:25:07

标签: maven initialization nullpointerexception execute mojo

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

/**
 * @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

1 个答案:

答案 0 :(得分:3)

问题解决了!!!

我输入了mvn help:有效的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 ......

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

    @Parameter(required = true)
    private String baseUrl;

...等

然后在插件描述符

中填充参数元素