【发布时间】:2013-09-19 15:49:09
【问题描述】:
我编写了一个 Maven 插件,并将其作为目标并入另一个项目的打包阶段,该项目的 pom.xml 中给出了配置。但是,使用 @parameter 表示法设置的字段最终都不会被填充,因此它们只会在使用时抛出 NullPointerExceptions。
我的魔力:
/**
* @goal wrap
* @phase package
*/
public class MyMojo extends AbstractMojo {
/**
* @parameter expression="${project.build.directory}"
*/
private String outputDirectory;
/**
* @parameter
*/
private String dbDataName;
private File dbFile;
public MyMojo(){
dbFile = new File(outputDirectory, dbDataName) // throws nullpointerexception
}
public void execute() throws MojoExecutionException{
// Do stuff
}
}
一些mojo pom:
<groupId>com.mycompany.wrapper</groupId>
<artifactId>something-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我的项目pom的相关位:
<plugin>
<groupId>com.mycompany.wrapper</groupId>
<artifactId>something-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>wrap</goal>
</goals>
<configuration>
<dbDataName>dcbTestData.sql</dbDataName>
</configuration>
</execution>
</executions>
</plugin>
谁能看到我在这里做错了什么?很可能是我没有看到的一些愚蠢的错误。
【问题讨论】:
-
首先我从未听说过名为
build的构建生命周期阶段?除此之外,maven-plugin-plugin 的配置看起来真的很错误..你在 github 上的某个地方有这个项目吗? -
啊,感谢您指出这一点 - 我错误地编辑了
maven-plugin-plugin部分的一部分,然后进一步试图纠正它!我现在已经纠正了这个问题,但我仍然遇到同样的问题,即我的 mojo 中没有初始化字段。帖子已更新。 -
您还没有将 dbFile 定义为参数,因此它不会被 Maven 初始化,也不会被您在配置中提供的参数初始化,也不能因为它没有被定义为参数。
-
谢谢。实际上,在为 stackoverflow 帖子调整我的代码时这是一个错误,它的意思是阅读
dbFile = new File(outputDirectory, dbDataName),所以 dbFile 并不是一个参数。我已经在我的帖子中更正了。
标签: maven maven-plugin pom.xml