在 Maven 插件中找到主要工件的路径
Find the path to the main artifact in a Maven plugin
我正在开发一个 Maven 插件来自动在服务器上安装 JAR。我的第一步应该是找到编译后的 JAR 并将其移动到服务器,但是如何找到我的 JAR?
我的第一次尝试是:
public class MyMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
String workingPath = System.getProperty("user.dir");
File targetDir = new File(workingPath + File.separatorChar + "target");
for (File f : targetDir.listFiles()) {
if (f.isFile() && f.getName().endsWith(".jar") {
System.out.println("Found jar " + f.getName());
}
}
}
}
但我不想使用固定目录"target"。有什么方法可以知道jar 将在哪里构建?
您可以使用 predefined variables project.build.directory
、project.build.finalName
和 project.packaging
的值来构建存档路径作为 默认值 你的插件的一个参数,比如:
public class MyMojo extends AbstractMojo {
/**
* @parameter default-value="${project.build.directory}/${project.build.finalName}.${project.packaging}"
*/
private String archivePath;
public void execute() throws MojoExecutionException {
File archive = new File(archivePath);
...
}
}
是的,执行此操作的简单方法是注入 MavenProject
that is currently being built, and retrieve the file corresponding to its artifact, as returned by getArtifact()
。这适用于任何项目的打包(jar
、war
等),或者工件实际生成的位置。
@Mojo(name = "foo")
public class MyMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// this is the main artifact file
File file = project.getArtifact().getFile();
getLog().info(file.toString());
}
}
这宣布了名为 "foo"
的目标。它被注入值 ${project}
,它由 PluginParameterExpressionEvaluator
.
计算到当前 Maven 项目
您的 Maven 插件将需要依赖 maven-plugin-annotations
(for the @Parameter
and @Mojo
注释),并且 maven-core
用于 MavenProject
:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5</version>
<scope>provided</scope>
</dependency>
当然,这需要实际构建项目的工件,也就是说插件必须在 package
阶段之后 运行;否则没有文件到工件。
我正在开发一个 Maven 插件来自动在服务器上安装 JAR。我的第一步应该是找到编译后的 JAR 并将其移动到服务器,但是如何找到我的 JAR?
我的第一次尝试是:
public class MyMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
String workingPath = System.getProperty("user.dir");
File targetDir = new File(workingPath + File.separatorChar + "target");
for (File f : targetDir.listFiles()) {
if (f.isFile() && f.getName().endsWith(".jar") {
System.out.println("Found jar " + f.getName());
}
}
}
}
但我不想使用固定目录"target"。有什么方法可以知道jar 将在哪里构建?
您可以使用 predefined variables project.build.directory
、project.build.finalName
和 project.packaging
的值来构建存档路径作为 默认值 你的插件的一个参数,比如:
public class MyMojo extends AbstractMojo {
/**
* @parameter default-value="${project.build.directory}/${project.build.finalName}.${project.packaging}"
*/
private String archivePath;
public void execute() throws MojoExecutionException {
File archive = new File(archivePath);
...
}
}
是的,执行此操作的简单方法是注入 MavenProject
that is currently being built, and retrieve the file corresponding to its artifact, as returned by getArtifact()
。这适用于任何项目的打包(jar
、war
等),或者工件实际生成的位置。
@Mojo(name = "foo")
public class MyMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// this is the main artifact file
File file = project.getArtifact().getFile();
getLog().info(file.toString());
}
}
这宣布了名为 "foo"
的目标。它被注入值 ${project}
,它由 PluginParameterExpressionEvaluator
.
您的 Maven 插件将需要依赖 maven-plugin-annotations
(for the @Parameter
and @Mojo
注释),并且 maven-core
用于 MavenProject
:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5</version>
<scope>provided</scope>
</dependency>
当然,这需要实际构建项目的工件,也就是说插件必须在 package
阶段之后 运行;否则没有文件到工件。