【问题标题】:Maven Plugin Development - Include Current Project's Resources to Plugin - "include-project-dependencies" - "NoSuchElementException"Maven 插件开发 - 将当前项目的资源包含到插件中 - “include-project-dependencies” - “NoSuchElementException”
【发布时间】:2013-03-03 09:30:44
【问题描述】:

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

"未能执行目标 sample.plugin:maven-plugin:1.0-SNAPSHOT:convertproperties (默认) on 项目临时:无法检索组件配置器 用于配置 mojo 的 include-project-dependencies sample.plugin:maven-plugin:1.0-SNAPSHOT:convertproperties: java.util.NoSuchElementException 角色:org.codehaus.plexus.component.configurator.ComponentConfigurator roleHint: 包含项目依赖项"

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>

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

【问题讨论】:

    标签: maven maven-plugin resourcedictionary mojo


    【解决方案1】:

    我找到了一个很好的解决方案来使用 maven-plugin 中的项目资源。 首先我放弃了使用suggestion。 我的插件的目的是转换一些资源(.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 --> y.z 我说打开是因为 maven 的工作与 tihs 完全一样;在构建时打开为某些 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。

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

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2015-08-31
      • 1970-01-01
      相关资源
      最近更新 更多