java 获取maven插件中本地存储库的路径

jv4diomz  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(188)

我是新的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;

}


有什么想法吗?
谢谢你

b1zrtrql

b1zrtrql1#

下面是一个解决方案:

MOJO类

package com.acme;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.RepositorySystemSession;

@Mojo(name = "dummy", requiresProject = false)
public class DummyMojo extends AbstractMojo
{
    @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
    protected RepositorySystemSession repoSession;
    
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException
    {
        getLog().info("" + repoSession.getLocalRepository().getBasedir());
    }
}

字符串

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.acme</groupId>
    <artifactId>dummy-maven-plugin</artifactId>
    <version>1-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>
    
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <build>
        <defaultGoal>install</defaultGoal>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.10.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-plugin-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.8.7</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.8.7</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>


在运行mvn install之后,我可以使用mvn com.acme:dummy-maven-plugin:1-SNAPSHOT:dummy执行插件,并且它正确地输出路径到我的本地存储库。

相关问题