Skip to content

Commit

Permalink
Issue 42
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 27, 2012
2 parents 5f82c3d + 34ac342 commit 7c9b55a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<artifactId>maven-processor-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>2.0.7-SNAPSHOT</version>
<name>maven-processor-plugin Maven Mojo</name>
<name>maven-processor-plugin - ${project.version}</name>
<description>A maven plugin to process annotation for jdk6 at compile time

This plugin helps to use from maven the new annotation processing provided by JDK6 integrated in java compiler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package org.bsc.maven.plugin.processor;

import java.io.File;
import java.io.IOException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -140,19 +141,45 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo

private static final Lock syncExecutionLock = new ReentrantLock();

/**
* additional source directories for the annotation processors.
* @parameter
*/
private File[] additionalSourceDirectories;

public File[] getAdditionalSourceDirectories() {
return additionalSourceDirectories;
}
/**
* additional source directories for the annotation processors.
* @parameter
*/
private File[] additionalSourceDirectories;

public File[] getAdditionalSourceDirectories() {
if( additionalSourceDirectories == null ) {
additionalSourceDirectories = new File[0];
}
return additionalSourceDirectories;
}

protected abstract File getSourceDirectory();
/**
*
* @return supported source directories
*/
protected abstract java.util.Set<File> getSourceDirectories();

/**
*
* @return output folder
*/
protected abstract File getOutputClassDirectory();

/**
*
* @param project
* @param dir
*/
protected abstract void addCompileSourceRoot(MavenProject project, String dir);

/**
*
* @return
*/
public abstract File getDefaultOutputDirectory();


private String buildProcessor()
{
if (processors == null || processors.length == 0)
Expand Down Expand Up @@ -249,14 +276,40 @@ private void executeWithExceptionsHandled() throws Exception

// new Debug(project).printDebugInfo();

java.io.File sourceDir = getSourceDirectory();
List<File> files = scanSourceDirectorySources(sourceDir);
for (File additionalSourceDir : getAdditionalSourceDirectories()) {
files.addAll(scanSourceDirectorySources(additionalSourceDir));
final String includesString = ( includes==null || includes.length==0) ? "**/*.java" : StringUtils.join(includes, ",");
final String excludesString = ( excludes==null || excludes.length==0) ? null : StringUtils.join(excludes, ",");

java.util.Set<File> sourceDirs = getSourceDirectories();
if( sourceDirs == null ) throw new IllegalStateException("getSourceDirectories is null!");

if( additionalSourceDirectories != null && additionalSourceDirectories.length>0) {
sourceDirs.addAll( Arrays.asList((File[])additionalSourceDirectories) );
}
Iterable< ? extends JavaFileObject> compilationUnits1 = null;

List<File> files = new java.util.ArrayList<File>();

for( File sourceDir : sourceDirs ) {

getLog().debug( String.format( "processing source directory [%s]", sourceDir.getPath()) );

if( sourceDir==null ) {
getLog().warn( "source directory is null! Processor task will be skipped!" );
continue;
}
if( !sourceDir.exists() ) {
getLog().warn( String.format("source directory [%s] doesn't exist! Processor task will be skipped!", sourceDir.getPath()));
continue;
}
if( !sourceDir.isDirectory() ) {
getLog().warn( String.format("source directory [%s] is invalid! Processor task will be skipped!", sourceDir.getPath()));
continue;
}


files.addAll( FileUtils.getFiles(sourceDir, includesString, excludesString) );
}

Iterable< ? extends JavaFileObject> compilationUnits1 = null;

String compileClassPath = buildCompileClasspath();

Expand Down Expand Up @@ -379,27 +432,27 @@ public void report(Diagnostic< ? extends JavaFileObject> diagnostic)
}

}

private List<File> scanSourceDirectorySources(File sourceDir) throws IOException {
if( sourceDir==null ) {
getLog().warn( "source directory cannot be read (null returned)! Processor task will be skipped");
return null;
}
if( !sourceDir.exists() ) {
getLog().warn( "source directory doesn't exist! Processor task will be skipped");
return null;
}
if( !sourceDir.isDirectory() ) {
getLog().warn( "source directory is invalid! Processor task will be skipped");
return null;
}

final String includesString = ( includes==null || includes.length==0) ? "**/*.java" : StringUtils.join(includes, ",");
final String excludesString = ( excludes==null || excludes.length==0) ? null : StringUtils.join(excludes, ",");

List<File> files = FileUtils.getFiles(sourceDir, includesString, excludesString);
return files;
}

private List<File> scanSourceDirectorySources(File sourceDir) throws IOException {
if( sourceDir==null ) {
getLog().warn( "source directory cannot be read (null returned)! Processor task will be skipped");
return null;
}
if( !sourceDir.exists() ) {
getLog().warn( "source directory doesn't exist! Processor task will be skipped");
return null;
}
if( !sourceDir.isDirectory() ) {
getLog().warn( "source directory is invalid! Processor task will be skipped");
return null;
}

final String includesString = ( includes==null || includes.length==0) ? "**/*.java" : StringUtils.join(includes, ",");
final String excludesString = ( excludes==null || excludes.length==0) ? null : StringUtils.join(excludes, ",");

List<File> files = FileUtils.getFiles(sourceDir, includesString, excludesString);
return files;
}

private void addCompilerArguments(List<String> options)
{
Expand Down Expand Up @@ -438,9 +491,6 @@ private void addOutputToSourcesIfNeeded()
}
}

protected abstract void addCompileSourceRoot(MavenProject project, String dir);
public abstract File getDefaultOutputDirectory();

private void ensureOutputDirectoryExists()
{
final File f = outputDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ public class MainAnnotationProcessorMojo extends AbstractAnnotationProcessorMojo
private File outputClassDirectory;

@Override
public File getSourceDirectory()
public java.util.Set<File> getSourceDirectories()
{
return sourceDirectory;
java.util.List<String> sourceRoots = project.getCompileSourceRoots();
java.util.Set<File> result = new java.util.HashSet<File>( sourceRoots.size() + 1);

result.add( sourceDirectory );
for( String s : sourceRoots ) {
result.add( new File(s) );
}

return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,17 @@ public class TestAnnotationProcessorMojo extends AbstractAnnotationProcessorMojo
private File outputClassDirectory;

@Override
public File getSourceDirectory()
public java.util.Set<File> getSourceDirectories()
{
return sourceDirectory;
java.util.List<String> sourceRoots = project.getCompileSourceRoots();
java.util.Set<File> result = new java.util.HashSet<File>( sourceRoots.size() + 1);

result.add( sourceDirectory );
for( String s : sourceRoots ) {
result.add( new File(s) );
}

return result;
}

protected void addCompileSourceRoot(MavenProject project, String dir)
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/bsc/maven/plugin/processor/ProcessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.bsc.maven.plugin.processor;

import org.junit.Assert;
import org.junit.Test;
import org.hamcrest.core.*;
/**
*
* @author softphone
*/
public class ProcessorTest {


@Test
public void testDuplicatePath() {

String homeFolder = System.getProperty("user.home");

java.io.File f1 = new java.io.File( homeFolder );

java.io.File f2 = new java.io.File( homeFolder );

java.io.File f3 = new java.io.File( homeFolder, ".m2" );

java.util.Set<java.io.File> fileSet = new java.util.HashSet<java.io.File>();


fileSet.add( f1 );
fileSet.add( f2 );


Assert.assertThat( fileSet.size(), IsEqual.equalTo(1) );

fileSet.add( f2 );
fileSet.add(f3);

Assert.assertThat( fileSet.size(), IsEqual.equalTo(2) );

}
}

0 comments on commit 7c9b55a

Please sign in to comment.