【问题标题】:mojo configuration not initialized before execute in custom maven plugin在自定义 maven 插件中执行之前未初始化 mojo 配置
【发布时间】:2012-11-14 05:32:12
【问题描述】:

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

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

【问题讨论】:

  • 只是为了清除为您的参数添加默认值并重新检查。确保这不仅仅是一个错字等。此外,如果您可以发布您的 pom.xml (您使用哪个 Maven 版本?),您是否有任何集成测试?或者您是否出于测试目的手动将插件安装到本地存储库中?
  • (对不起,cmets部分空间不够,请看问题部分本身,我已经添加了您要求的资源)

标签: maven initialization nullpointerexception execute mojo


【解决方案1】:

问题解决了!!!

我输入 mvn help:effective pom 发现 maven-plugin-plugin 版本是 2.7

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

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

我升级了我的 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;

...等

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2013-12-11
    • 2015-12-01
    相关资源
    最近更新 更多