gpt4 book ai didi

maven - 无法在 Maven 自定义插件中将目录设置为资源/源

转载 作者:行者123 更新时间:2023-12-05 05:46:13 42 4
gpt4 key购买 nike

我无法使用最常见的代码将文件夹设置为源/资源,从 Apache 存储库复制并粘贴到自定义 Maven 插件中:

主要/魔力:

package com.example;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;

import java.io.File;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
* Goal that sets directory as source/resource.
*/
@Mojo(name = "set-some-dir-as-source", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class SetDirectoryAsSourceMojo extends AbstractMojo {

@Component
private MavenSession mavenSession;

@Component
private BuildPluginManager pluginManager;

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

/**
* Output location.
*/
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/some-folder/generated")
protected File outputDirectory;

/**
* Main entry into mojo.
*
* @throws MojoExecutionException
* @throws MojoFailureException
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Executing Mark Directory as Source");

executeMojo(
plugin(
groupId("org.codehaus.mojo"),
artifactId("build-helper-maven-plugin"),
version("3.3.0")
),
goal("add-source"),
configuration(
element(name("sources"),
element(name("source"), getOutputDirectory().getPath())
)
),
executionEnvironment(
project,
mavenSession,
pluginManager
)
);

getLog().info("Executing Mark Directory as Resource");

executeMojo(
plugin(
groupId("org.codehaus.mojo"),
artifactId("build-helper-maven-plugin"),
version("3.3.0")
),
goal("add-resource"),
configuration(
element(name("resources"),
element(name("resource"),
element(name("directory"), getOutputDirectory().getPath())
)
)
),
executionEnvironment(
project,
mavenSession,
pluginManager
)
);

project.addCompileSourceRoot(getOutputDirectory().getPath());
project.addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
if (getLog().isInfoEnabled()) {
getLog().info("Source directory: " + getOutputDirectory().getPath() + " added.");
getLog().info("Source directory: " + getOutputDirectory().getAbsolutePath() + " added.");
}

Resource resource = new Resource();
resource.setTargetPath(getOutputDirectory().getPath());
resource.setTargetPath(getOutputDirectory().getAbsolutePath());
getProject().addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
if (getLog().isInfoEnabled()) {
getLog().info("Resource directory: " + getOutputDirectory().getPath() + " added.");
getLog().info("Resource directory: " + getOutputDirectory().getAbsolutePath() + " added.");
}

}

/**
* @return Returns the Maven project.
*/
public MavenProject getProject() {
return project;
}

/**
* @return Returns the outputDirectory.
*/
public File getOutputDirectory() {
return this.outputDirectory;
}

/**
* @param theOutputDirectory The outputDirectory to set.
*/
public void setOutputDirectory(File theOutputDirectory) {
this.outputDirectory = theOutputDirectory;
}

}

在上面的示例中,我使用的 executeMojo 来自:

https://github.com/mojo-executor/mojo-executor

并且还在最后几行中以编程方式进行。

项目pom.xml:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>custom-set-source-maven-plugin</artifactId>
<version>0.0.1</version>
<packaging>maven-plugin</packaging>

<name>custom-set-source-maven-plugin</name>

<url>http://maven.apache.org</url>

<organization>
<name>The Example</name>
<url>https://www.example.com/</url>
</organization>

<properties>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven-plugin-api.version>3.8.4</maven-plugin-api.version>
<maven-plugin-annotations.version>3.6.4</maven-plugin-annotations.version>
<maven-project.version>2.2.1</maven-project.version>
<maven-plugin-plugin.version>3.6.4</maven-plugin-plugin.version>
<maven-site-plugin.version>3.11.0</maven-site-plugin.version>
<maven-dependency-plugin.version>3.2.0</maven-dependency-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven-plugin-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven-plugin-annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven-project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.twdata.maven</groupId>
<artifactId>mojo-executor</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven-plugin-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven-site-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>

</project>

如果我只是在我的项目中从 org.codehaus.mojo:build-helper-maven-plugin 声明插件,它就可以工作:

    <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-element-templates-as-source</id>
<phase>generate-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/processes/some_folder/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

我的自定义插件中是否缺少任何描述符?一种告诉 IDE 我缺少的目录已设置的方法?我似乎无法在 Mojohaus 源代码中找到任何内容:

https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java

https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java

最佳答案

  1. 在构建时我看到了以下错误:
[ERROR] 

Some dependencies of Maven Plugins are expected to be in provided scope.
Please make sure that dependencies listed below declared in POM
have set '<scope>provided</scope>' as well.

The following dependencies are in wrong scope:
* org.apache.maven:maven-plugin-api:jar:3.8.4:compile
* org.apache.maven:maven-model:jar:3.8.4:compile
* org.apache.maven:maven-artifact:jar:3.8.4:compile
* org.apache.maven:maven-project:jar:2.2.1:compile
* org.apache.maven:maven-settings:jar:2.2.1:compile
* org.apache.maven:maven-profile:jar:2.2.1:compile
* org.apache.maven:maven-artifact-manager:jar:2.2.1:compile
* org.apache.maven:maven-plugin-registry:jar:2.2.1:compile
* org.apache.maven:maven-core:jar:3.1.1:compile
* org.apache.maven:maven-settings-builder:jar:3.1.1:compile
* org.apache.maven:maven-model-builder:jar:3.1.1:compile
* org.apache.maven:maven-repository-metadata:jar:3.1.1:compile
* org.apache.maven:maven-aether-provider:jar:3.1.1:compile

需要对 POM 文件进行一些更改才能使其正常工作。

  1. Maven Session 上方的组件注释出现警告。更改如下:
    @Parameter( defaultValue = "${session}", readonly = true )
private MavenSession mavenSession;

经过上述更改后,可以使用以下命令编译和执行代码:

mvn install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install
  1. 出于某种原因,代码提示 outputDirectory。

我在 https://github.com/gopinnath/custom-set-source-maven-plugin 有固定密码

可以用下面的maven命令执行:

install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install exec:java -Dexec.mainClass="Example"

关于maven - 无法在 Maven 自定义插件中将目录设置为资源/源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71214163/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com