1

我正在尝试开发一个 maven 插件,该插件使用将其用作插件的项目的资源文件。我将该建议实施到我的 maven-plugin 项目中,并且构建得很好。但是当我清理并构建使用该插件的项目时,我得到了这个异常:

“无法在项目临时执行目标 sample.plugin:maven-plugin:1.0-SNAPSHOT:convertproperties(默认):无法检索组件配置器 include-project-dependencies 以配置 mojo sample.plugin:maven-plugin:1.0-SNAPSHOT :convertproperties: java.util.NoSuchElementException 角色:org.codehaus.plexus.component.configurator.ComponentConfigurator 角色提示:include-project-dependencies"

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>sample.plugin</groupId>
    <artifactId>maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>



    <name>maven-plugin</name>

    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-utils</artifactId>
            <version>[1.5.0, 1.5.1 ]</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>2.0.9</version>
            <exclusions>
                <exclusion>
                    <artifactId>maven-artifact</artifactId>
                    <groupId>org.apache.maven</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>2.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>2.0.9</version>
            <exclusions>
                <exclusion>
                    <artifactId>maven-artifact</artifactId>
                    <groupId>org.apache.maven</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>maven-repository-metadata</artifactId>
                    <groupId>org.apache.maven</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>wagon-provider-api</artifactId>
                    <groupId>org.apache.maven.wagon</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-artifact</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-repository-metadata</artifactId>
            <version>2.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-provider-api</artifactId>
            <version>1.0-beta-2</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>descriptor</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-plugin-plugin</artifactId>
                    <version>2.3</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>2.4</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

我使用 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.mycompany</groupId>
    <artifactId>temp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>temp</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.3</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>sample.plugin</groupId>
                <artifactId>maven-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>convertproperties</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourcePath>/lang</sourcePath>
                    <destinationPath>lang_json</destinationPath>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>src\main\webapp\lang</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

</project>

任何帮助都会非常有用,谢谢。

4

1 回答 1

1

我找到了一个很好的解决方案来使用 maven-plugin 中的项目资源。首先,我放弃了使用该建议。我的插件的目的是转换一些资源(.properties)文件并将它们保存到项目中存储 jsp 页面的位置。因此,第一步是复制所需的资源文件并根据需要使用另一个名为“maven-antrun-plugin”的 maven-plugin 重命名它们,方法是这样设置:

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>CopyLanguagePropertiesAsJson</id>
                    <phase>compile</phase>
                    <configuration>
                        <tasks>
                            <copy file="${basedir}\src\main\resources\x.properties"
                                  tofile="${basedir}\src\main\webapp\x\y.z" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

在那次移动之后,我将打开的属性文件和目标文件作为变量发送到我的插件中,键入“文件”并转换属性文件并存储到转换后的文件中 ==> x.properties --> yz 我说打开是因为 maven 做了工作完全一样;在构建时打开为某些 Maven 插件设置为变量的文件,并发送那些作为这些文件引用的对象。maven-plugin 可以通过这种方式更改这些文件。

像这样:

        <plugin>
            <groupId>group.id</groupId>
            <artifactId>artifact-id</artifactId>
            <version>x.x.x</version>
            <configuration>
                <properties>
                    <file>${basedir}\src\main\resources\x.properties</file>
                </properties>
                <convertedFiles>
                    <file>${basedir}\src\main\webapp\x\y.z</file>
                </jsons>
            </convertedFiles>
        </plugin>

这些文件是在我的插件的代码(类扩展 AbstractMojo)中获取的,如下所示:

/**
 * @parameter 
 */
private File[] properties;
/**
 * @parameter 
 */
private File[] convertedFiles;

这些是完全实现将 .properties 文件转换为另一个文件并存储在项目某处的目标的步骤。

此外,我遇到了一个异常,它告诉在使用它的项目的构建时间执行 maven-plugin 时找不到某些类。这是由于在当前项目的构建时无法找到所需的 maven-plugin jar 文件造成的。解决方法是使用名为“maven-assembly-plugin”的插件来构建 maven-plugin 的 jar,包括 maven-plugin 使用的依赖 jar 库。maven-plugin 的 pom 文件中的插件配置:

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

如果在某些情况下,插件无法按照描述编译您的 jar,可能是由于您的 maven-plugin 中不包含主类。因为使用这种将所有jar文件编译为一个的插件的原因是可以单独执行当前项目的jar,而不需要运行时的任何其他类(构建独立的jar文件)。因此,只需创建一个使用 maven-plugin 类的主类(类扩展 AbstractMojo)并尝试再次编译。成功编译后,您可以删除主类,它会在没有主类的情况下再次成功构建您的 maven-plugin。

如果这篇文章对某人有帮助,我将非常高兴。

于 2013-03-18T09:12:11.973 回答