我们需要编写一个maven插件
下载依赖逻辑按预期工作。
但是,当项目在intellij中打开时, 下载的库不在项目编译类路径中。
在github上附上一个非常简化的项目来重现问题:-
https://github.com/moglideveloper/download-dependencies-from-maven-plugin-example
执行以下命令重现问题:-
git clone https://github.com/moglideveloper/download-dependencies-from-maven-plugin-example
cd download-dependencies-maven-plugin
mvn install
cd ../example-project
mvn clean compile
下面是DownloadDependencyMojo配置:-
public void execute() throws MojoExecutionException {
Artifact aetherArtifact = new DefaultArtifact(groupId, artifactId, type, version);
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(aetherArtifact);
artifactRequest.setRepositories(remoteRepos);
try {
ArtifactResult aetherArtifactResult = repoSystem.resolveArtifact(repoSession, artifactRequest);
Artifact resolvedAetherArtifact = aetherArtifactResult.getArtifact();
getLog().info("Resolved aetherArtifact: " + resolvedAetherArtifact.getFile().getPath());
project.addCompileSourceRoot(resolvedAetherArtifact.getFile().getAbsolutePath());
org.apache.maven.artifact.DefaultArtifact defaultArtifact = toMavenDefaultArtifact(resolvedAetherArtifact, "compile");
project.addAttachedArtifact(defaultArtifact);
Dependency compileScopeDependency = toDependency(resolvedAetherArtifact, "compile");
project.getDependencies().add(compileScopeDependency);
getLog().info("added to project: " + resolvedAetherArtifact.getFile().getPath());
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException("failed to download dependencies", e);
}
}
❯ mvn --version
Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)
Maven home: /Users/mogli/.sdkman/candidates/maven/current
Java version: 17.0.6, vendor: Eclipse Adoptium, runtime: /Users/mogli/.sdkman/candidates/java/17.0.6-tem
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "12.6.1", arch: "x86_64", family: "mac"
您面临的问题似乎是在 IntelliJ 中打开时,您的自定义 Maven 插件下载的依赖项未添加到项目的编译类路径中。以下是您可以尝试解决此问题的一些方法:
dependency:copy-dependencies
目标将所有项目依赖项下载到指定目录。下载依赖项后,您可以使用 maven-jar-plugin
将它们添加到项目的编译类路径中。这是您可以使用的示例配置:<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
手动将下载的依赖添加到 IntelliJ 的类路径中 您也可以尝试在 IntelliJ 中手动将下载的依赖项添加到项目的类路径中。为此,右键单击项目资源管理器中的项目,选择“打开模块设置”,然后导航到“库”选项卡。点击“+”按钮添加新库,选择“Java”然后选择“From Maven...”,输入下载的依赖坐标。 IntelliJ 应该会自动下载依赖项并将其添加到项目的类路径中。
使用 Maven IntelliJ IDEA 插件 另一种选择是使用 Maven IntelliJ IDEA 插件。这个插件提供了几个目标,可用于从 Maven 项目生成 IntelliJ IDEA 项目文件。目标之一,
idea:module
,可用于生成包含项目所有依赖项的模块文件。这是您可以使用的示例配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-idea-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>module</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
生成模块文件后,您可以通过选择“文件”>“打开”并选择生成的
*.iml
文件在IntelliJ中打开它。这应该包括 IntelliJ 类路径中项目的所有依赖项。