我是新的maven和我试图建立我的第一个maven插件,在我的插件,我想确定的路径项目的依赖,但我没有看到无论如何访问路径的localRepository
.
我尝试了以下方法:
MavenSession.getLocalRepository().getUrl()
个MavenSession.getRequest().getLocalRepositoryPath().getAbsolutePath()
个MavenSession.getRepositorySession().getLocalRepository().getBasedir().getAbsolutePath()
个
但是它们在调用getLocalRepository()
时都返回null,我也在网上到处搜索,但没有找到任何关于它是如何完成的线索。
这里是我运行我的插件的项目的pom:
<?xml version="1.0" encoding="UTF-8"?>
<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.evalia.tools</groupId>
<artifactId>evalia-project</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Evalia project</name>
<properties>
<author>Developer one</author>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.evalia.tools</groupId>
<artifactId>tools-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
字符串
这是我的基本目标,其他目标应该扩展:
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
/**
* Loads project dependencies which is run on.
*/
public abstract class AbstractDependencyLoaderMojo extends AbstractMojo {
/**
* the pom model as MavenProject
*/
@Parameter(defaultValue = "${session}", property = "session", required = true, readonly = true)
private MavenSession session;
/**
* filters dependencies to be loaded using their groupIds
*/
@Parameter(defaultValue = "", property = "groups", required = false)
private String groupIds;
/**
* filters dependencies to be loaded using their artifactIds
*/
@Parameter(defaultValue = "", property = "artifacts", required = false)
private String artifactIds;
private void injectDependencies(MavenProject project, String repoPath) throws MojoFailureException, MojoExecutionException {
List<Dependency> dependencies = project.getDependencies();
GroupIdFilter groupIdFilter = new GroupIdFilter(groupIds);
ArtifactIdFilter artifactIdFilter = new ArtifactIdFilter(artifactIds);
if(groupIdFilter.isPresent()){
dependencies.removeIf(groupIdFilter);
}
if(artifactIdFilter.isPresent()){
dependencies.removeIf(artifactIdFilter);
}
List<URL> urls = new ArrayList<>();
for(Dependency dependency: dependencies){
urls.add(ClasspathUtils.mapToUrl(repoPath, dependency));
}
updateClasspath(urls.toArray(new URL[urls.size()]));
}
@SuppressWarnings("resource")
private void updateClasspath(URL... urls) throws MojoExecutionException{
try {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
URLClassLoader urlClassLoader = new URLClassLoader(urls, parent);
Thread.currentThread().setContextClassLoader(urlClassLoader);
} catch (Exception e) {
throw new MojoExecutionException("Something went wrong, " +
"see the below stacktrace for more information!", e);
}
}
public void execute() throws MojoFailureException, MojoExecutionException {
MavenProject project = session.getTopLevelProject();
String localRepoPath = null; //Here i want to resolve the path to the local repository
try {
injectDependencies(project, localRepoPath);
postExecute(project);
} catch (Exception e) {
throw new MojoFailureException("Something went wrong", e);
}
}
public abstract void postExecute(MavenProject project) throws MojoFailureException, MojoExecutionException;
}
型
有什么想法吗?
谢谢你
1条答案
按热度按时间b1zrtrql1#
下面是一个解决方案:
MOJO类
字符串
POM
型
在运行
mvn install
之后,我可以使用mvn com.acme:dummy-maven-plugin:1-SNAPSHOT:dummy
执行插件,并且它正确地输出路径到我的本地存储库。