maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jackett_dad <jackett_...@yahoo.com>
Subject Re: generate-sources target called twice
Date Tue, 20 Mar 2012 14:27:01 GMT
My application isn't special as it pertains to Maven.  The idea is that there
are interfaces within my source code that are annotated with a custom
annotation.  During the generate sources phase, the custom annotation
processor that I've written inspects the marked-up interfaces, generates
code, then the generated code is compiled and bundled with the other
compiled classes.  There is nothing unique about this scenario.

To demonstrate this completely, I've reproduced the entire scenario in two
small projects.  The custom annotations needs to be in its own project and
imported as a dependency to the project that will employ it during its
compilation phase.  I called this project CustomAnnotationProcessorTest.  It
defines a simple annotation and a simple annotation processor.

Annotation:
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {
}

Processor:
@SupportedAnnotationTypes({"test.annotations.CustomAnnotation"})
public class CustomProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
"Processing annotation CustomAnnotation in CustomProcessor");
        return true;
    }
}

POM:

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

    <groupId>test</groupId>
    <artifactId>custom-annotation-processor-test</artifactId>
    <version>1.0</version>

    <packaging>jar</packaging>

    <properties>
        <jdk.version>1.6</jdk.version>
        <maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin-version}</version>
                <configuration>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <compilerVersion>${jdk.version}</compilerVersion>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>UTF-8</encoding>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

The other project has a single interface in it that annotates with the
CustomAnnotation shown above.

Interface:
@CustomAnnotation
public interface IRemoteService {
    public Object serviceMethod1(int parm1, double parm2);
    public Integer serviceMethod2(Float parm1, Object parm2);
}

POM:

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

    <groupId>test</groupId>
    <version>1.0</version>
    <artifactId>custom-annotation-test</artifactId>

    <dependencies>
        <dependency>
            <groupId>test</groupId>
            <artifactId>custom-annotation-processor-test</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.5</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                           
<outputDirectory>target/generated-sources/annotations</outputDirectory>
                            <processors>
                               
<processor>test.processors.CustomProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The processor emits a message whenever it is called, which should only occur
once.  When I execute 
"mvn install" at the root of the consumer project, I see these messages
emitted from the custom processor:

[INFO] diagnostic Note: Processing annotation CustomAnnotation in
CustomProcessor
[INFO] diagnostic Note: Processing annotation CustomAnnotation in
CustomProcessor

I'm using IntelliJ as the IDE, but the output is the same if I run maven
from within my IDE or from the command line.




--
View this message in context: http://maven.40175.n5.nabble.com/generate-sources-target-called-twice-tp5577988p5580167.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Mime
View raw message