Dis*_*sha 12 java maven-plugin maven
我有一个在其类路径(Java buildpath)中具有依赖项的项目.现在我已将其转换为maven项目并使用我自定义的Maven插件进行编译.以下将是我的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ear</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.my.maven.plugin</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>0.0.1</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
在my-maven-plugin,我已经覆盖了编译阶段 -
maven-plugin 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>my-maven-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>maven-surefire-common</artifactId>
<version>2.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>2.19.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
lifecycle.xml -
<?xml version="1.0" encoding="UTF-8"?>
<lifecycles>
<lifecycle>
<id>customLifeCycle</id>
<phases>
<phase>
<id>compile</id>
<executions>
<execution>
<goals>
<goal>mycompile</goal>
</goals>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
Run Code Online (Sandbox Code Playgroud)
components.xml中
<?xml version="1.0" encoding="UTF-8"?>
<component-set>
<components>
<component>
<role>org.apache.maven.artifact.handler.ArtifactHandler</role>
<role-hint>ear</role-hint>
<implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
<configuration>
<type>ear</type>
<extension>ear</extension>
<language>java</language>
<packaging>ear</packaging>
</configuration>
</component>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>ear</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<phases>
<process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
<compile>com.test:my-maven-plugin:mycompile</compile>
<process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources>
<test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
<test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
<install>org.apache.maven.plugins:maven-install-plugin:install</install>
<deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>
</configuration>
</component>
</components>
</component-set>
Run Code Online (Sandbox Code Playgroud)
并且,重写编译mojo -
@Mojo( name = "mycompile", defaultPhase = LifecyclePhase.COMPILE )
public class MyCompileMojo extends AbstractMojo{
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在编译阶段,我想要一个存在于classpath中的JAR文件列表.我怎么才能得到它?我试过以下 - 但大多数都返回目标文件夹路径和类文件夹路径
List<String> list1 = project.getCompileClasspathElements();
List<String> list2 = project.getRuntimeClasspathElements();
List<String> list3 = project.getSystemClasspathElements();
List<String> list4 = project.getTestClasspathElements();
List<Dependency> list5 = project.getCompileDependencies();
Properties list6 = project.getProperties();
List<Dependency> list7 = project.getSystemDependencies();
List<Dependency> list8 = project.getRuntimeDependencies();
Run Code Online (Sandbox Code Playgroud)
我的项目结构要编译
您缺少的关键成分是requiresDependencyResolution,它代表将可供插件使用的依赖项集。来自Mojo API 参考:
将此 Mojo 标记为要求在执行之前解析指定类路径中的依赖项。[...] 如果注释根本不存在,那么 mojo 不得对与 Maven 项目关联的工件做出任何假设。
要访问项目的编译时依赖项,您可以使用ResolutionScope.COMPILE:
compile解析范围 =compile+system+provided依赖关系
除此之外,还COMPILE_PLUS_RUNTIME可以访问运行时范围的依赖项,或者TEST添加测试范围的依赖项。当 Maven 插件不存在此参数时,Maven 项目的类路径元素将不可用,这就是您遇到的行为。
因此,如果您希望 Maven 插件获取当前 Maven 项目的依赖项的编译 JAR 文件列表,您可以:
@Mojo(name = "mycompile", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE)
public class MyCompileMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
try {
List<String> list = project.getCompileClasspathElements();
getLog().info(list.toString());
} catch (DependencyResolutionRequiredException e) {
throw new MojoFailureException("Couldn't resolve compile dependencies", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
该方法getCompileClasspathElements()返回每个编译时依赖项的 JAR 文件的路径列表。它还将包含当前 Maven 项目的文件夹路径target/classes,因为它包含该项目已编译的主要 Java 源代码。上面的代码将简单地打印这个列表。同样,您可以将getTestClasspathElements()与 一起使用ResolutionScope.TEST以获得测试范围依赖项以及编译时依赖项的列表。
如果您只需要依赖项的 JAR 文件的路径,而不需要项目本身的类,则可以使用以下方法getArtifacts():
public void execute() throws MojoExecutionException, MojoFailureException {
Set<Artifact> artifacts = project.getArtifacts();
for (Artifact artifact : artifacts) {
System.out.println(artifact.getFile());
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,要使其正常工作,您需要确保阶段compile(或test-compile)已运行。这些列表是惰性填充的。运行mvn clean test将确保插件可以访问测试范围依赖项(如果它声明了 of requiresDependencyResolution)TEST。类似地,运行mvn clean compile(以及将阶段更改为<phase>compile</phase>)将确保插件可以访问编译范围依赖项(如果它声明了 of requiresDependencyResolution)COMPILE。
几个旁注:
lifecycle.xml必须位于内部src/main/resources/META-INF/maven。请注意,该文件实际上在您的设置中完全未使用,因为您没有customLifeCycle在, 中使用 new components.xml,而是覆盖ear;components.xml必须位于内部src/main/resources/META-INF/plexus。请注意,<compile>com.test:my-maven-plugin:mycompile</compile>必须与插件的组 ID 和工件 ID 相匹配;测试项目需要像这样使用插件:
<packaging>ear</packaging> <!-- because your lifecycle mapping override ear -->
<dependencies>
<!-- some dependencies like maven-core -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.test</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
然后您可以运行mvn clean package,插件将正确打印maven-core(例如)的 JAR 文件及其依赖项的路径。
这对我有用。
我在这里找到了它https://www.mkyong.com/java/how-to-print-out-the-current-project-classpath/
package com.mkyong.io;
import java.net.URL;
import java.net.URLClassLoader;
public class App{
public static void main (String args[]) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1133 次 |
| 最近记录: |