将maven插件执行绑定到仅列出的子模块而不是所有子模块

时间:2018-03-05 17:26:35

标签: java maven

您好我已经创建了一个maven插件

package com.common.gflogging.gen.parser;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

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.apache.maven.project.MavenProject;
import org.slf4j.Logger;

import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;

/**
 * This is where we take in java classes and check if they have loggers
 *
 */

@Mojo( name = "codeloggersparser" )
public class CodeLoggersParser extends AbstractMojo {

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

    private String tier;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

    File f = new File( "/home/xxx/Desktop/list" + "." + tier );
    if ( !f.exists() ) {
        try {
        f.createNewFile();
        } catch ( IOException e ) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }

    String currentSrcFolder = project.getCompileSourceRoots().iterator().next();
    JavaDocBuilder builder = new JavaDocBuilder();
    builder.addSourceTree( new File( currentSrcFolder ) );
    JavaClass[] classes = builder.getClasses();
    for ( JavaClass jClass : classes ) {
        if ( jClass.getName().contains( "AuthenticationConfiguration" ) ) {
        System.out.println( jClass.getName() );
        }
        for ( JavaField jFiled : jClass.getFields() ) {

        if ( jFiled.getType().getJavaClass().isA( new JavaClass( Logger.class.getName() ) ) ) {
            try {
            Files.write( f.toPath(), (jClass.getFullyQualifiedName() + "\n").getBytes(), StandardOpenOption.APPEND );
            } catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
        }
    }
    }

}

我有4个大型maven项目,每个项目都有这个maven插件在父pom中使用(它基本上是一个扫描代码的插件,我不想把它包含在每个孩子的pom中)。

4个父母之一。

<build>
        <plugins>

            <plugin>
                <groupId>com.common</groupId>
                <artifactId>gflogginggen</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>codeloggersparser</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!-- This is so we avoid M2E maven eclipse plugin not covered lifecycle messages -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>com.common</groupId>
                                        <artifactId>gflogginggen</artifactId>
                                        <versionRange>[0.0,)</versionRange>
                                        <goals>
                                            <goal>codeloggersparser</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

    </build>

    <modules>
        <module>1</module>
        <module>2</module>
        <module>3</module>
        <module>gflogginggen</module>
    </modules>

在超级pom中定义maven执行将在所有子子模块上执行,而不是我只希望列表模块执行它,这是可行的吗?

0 个答案:

没有答案