You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/01/22 19:09:17 UTC

[2/2] camel git commit: CAMEL-10711: Added automatic source roots addition in api maven plugin

CAMEL-10711: Added automatic source roots addition in api maven plugin


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b1943195
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b1943195
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b1943195

Branch: refs/heads/master
Commit: b19431955dd4cca939f44c8036e2537940832299
Parents: 06c6cd7
Author: Marco Collovati <(none)>
Authored: Sun Jan 22 17:19:57 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Jan 22 20:05:59 2017 +0100

----------------------------------------------------------------------
 .../src/it/all-it/pom.xml                       | 21 +-----
 .../src/it/settings.xml                         |  4 +
 .../maven/AbstractApiMethodGeneratorMojo.java   |  2 +
 .../maven/AbstractSourceGeneratorMojo.java      | 29 +++++++-
 .../camel/maven/ApiComponentGeneratorMojo.java  |  3 +-
 .../camel/maven/AbstractGeneratorMojoTest.java  | 77 ++++++++++++++++++--
 .../maven/ApiComponentGeneratorMojoTest.java    | 22 ++++--
 .../maven/FileApiMethodGeneratorMojoTest.java   | 20 +++--
 .../JavadocApiMethodGeneratorMojoTest.java      | 16 ++--
 9 files changed, 147 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/it/all-it/pom.xml
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/it/all-it/pom.xml b/tooling/maven/camel-api-component-maven-plugin/src/it/all-it/pom.xml
index d1d6449..b42922d 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/it/all-it/pom.xml
+++ b/tooling/maven/camel-api-component-maven-plugin/src/it/all-it/pom.xml
@@ -126,6 +126,7 @@
               <goal>fromApis</goal>
             </goals>
             <configuration>
+              <addCompileSourceRoots>test</addCompileSourceRoots>
               <apis>
                 <api>
                   <apiName>test</apiName>
@@ -189,26 +190,6 @@
           </execution>
         </executions>
       </plugin>
-      <!-- add generated source and test source to build for test-compile -->
-      <plugin>
-        <groupId>org.codehaus.mojo</groupId>
-        <artifactId>build-helper-maven-plugin</artifactId>
-        <version>@build-helper-maven-plugin-version@</version>
-        <executions>
-          <execution>
-            <id>add-generated-test-sources</id>
-            <goals>
-              <goal>add-test-source</goal>
-            </goals>
-            <configuration>
-              <sources>
-                <source>${project.build.directory}/generated-sources/camel-component</source>
-                <source>${project.build.directory}/generated-test-sources/camel-component</source>
-              </sources>
-            </configuration>
-          </execution>
-        </executions>
-      </plugin>
     </plugins>
     <pluginManagement>
       <plugins>

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/it/settings.xml
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/it/settings.xml b/tooling/maven/camel-api-component-maven-plugin/src/it/settings.xml
index c8f77f0..c0cc67b 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/it/settings.xml
+++ b/tooling/maven/camel-api-component-maven-plugin/src/it/settings.xml
@@ -26,6 +26,10 @@ under the License.
       <activation>
         <activeByDefault>true</activeByDefault>
       </activation>
+      <properties>
+        <maven.compiler.source>1.7</maven.compiler.source>
+        <maven.compiler.target>1.7</maven.compiler.target>
+      </properties>
       <repositories>
         <repository>
           <id>local.central</id>

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
index dc8d1d9..db16da0 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
@@ -52,6 +52,8 @@ public abstract class AbstractApiMethodGeneratorMojo extends AbstractApiMethodBa
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
 
+        setCompileSourceRoots();
+
         // load proxy class and get enumeration file to generate
         final Class proxyType = getProxyType();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractSourceGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractSourceGeneratorMojo.java b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractSourceGeneratorMojo.java
index add0616..7dc521d 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractSourceGeneratorMojo.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractSourceGeneratorMojo.java
@@ -16,10 +16,10 @@
  */
 package org.apache.camel.maven;
 
-import java.io.File;
-
 import org.apache.maven.plugins.annotations.Parameter;
 
+import java.io.File;
+
 /**
  * Base class for API based code generation MOJOs.
  */
@@ -30,4 +30,29 @@ public abstract class AbstractSourceGeneratorMojo extends AbstractGeneratorMojo
 
     @Parameter(defaultValue = "${project.build.directory}/generated-test-sources/camel-component")
     protected File generatedTestDir;
+
+    enum CompileRoots {
+        source, test, all, none
+    }
+
+    @Parameter(defaultValue = "all", property = PREFIX + "addCompileSourceRoots")
+    protected CompileRoots addCompileSourceRoots = CompileRoots.all;
+
+    protected void setCompileSourceRoots() {
+        switch (addCompileSourceRoots) {
+            case source:
+                project.addCompileSourceRoot(generatedSrcDir.getAbsolutePath());
+                project.addCompileSourceRoot(generatedTestDir.getAbsolutePath());
+                break;
+            case test:
+                project.addTestCompileSourceRoot(generatedSrcDir.getAbsolutePath());
+                project.addTestCompileSourceRoot(generatedTestDir.getAbsolutePath());
+                break;
+            case all:
+                project.addCompileSourceRoot(generatedSrcDir.getAbsolutePath());
+                project.addTestCompileSourceRoot(generatedTestDir.getAbsolutePath());
+                break;
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
index 960dc59..2aeab50 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
@@ -133,7 +133,7 @@ public class ApiComponentGeneratorMojo extends AbstractApiMethodBaseMojo {
             clearSharedProjectState();
         }
     }
-
+    
     private void configureMethodGenerator(AbstractApiMethodGeneratorMojo mojo, ApiProxy apiProxy) {
 
         // set AbstractGeneratorMojo properties
@@ -146,6 +146,7 @@ public class ApiComponentGeneratorMojo extends AbstractApiMethodBaseMojo {
         // set AbstractSourceGeneratorMojo properties
         mojo.generatedSrcDir = generatedSrcDir;
         mojo.generatedTestDir = generatedTestDir;
+        mojo.addCompileSourceRoots = addCompileSourceRoots;
 
         // set AbstractAPIMethodBaseMojo properties
         mojo.substitutions = apiProxy.getSubstitutions().length != 0

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/AbstractGeneratorMojoTest.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/AbstractGeneratorMojoTest.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/AbstractGeneratorMojoTest.java
index f374385..0fee442 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/AbstractGeneratorMojoTest.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/AbstractGeneratorMojoTest.java
@@ -6,7 +6,7 @@
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,14 +16,23 @@
  */
 package org.apache.camel.maven;
 
-import java.io.File;
-import java.util.Collections;
-import java.util.List;
-
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Model;
 import org.apache.maven.project.MavenProject;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -91,4 +100,62 @@ public abstract class AbstractGeneratorMojoTest {
             }
         };
     }
+
+    protected AbstractSourceGeneratorMojo createGeneratorMojo() {
+        return null;
+    }
+
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void shouldAddCompilationRootsByDefault() throws Exception {
+        AbstractSourceGeneratorMojo mojo = createGeneratorMojo();
+        Assume.assumeThat("Ignored because createGeneratorMojo is not implemented", mojo, notNullValue());
+        // Differentiate target folders to simplify assertion
+        mojo.generatedSrcDir = new File(OUT_DIR.replace("-test-", ""));
+        mojo.generatedTestDir = new File(OUT_DIR);
+        mojo.execute();
+
+        assertCompileSourceRoots(mojo.project::getCompileSourceRoots, mojo.generatedSrcDir);
+        assertCompileSourceRoots(mojo.project::getTestCompileSourceRoots, mojo.generatedTestDir);
+    }
+
+    @Test
+    public void shouldAddCompilationRootsByConfiguration() throws Exception {
+        File srcDir = new File(OUT_DIR.replace("-test-", ""));
+        File testDir = new File(OUT_DIR);
+        File[] empty = new File[0];
+        assertCompilationRootsByConfiguration(AbstractSourceGeneratorMojo.CompileRoots.source, srcDir, testDir,
+            new File[]{srcDir, testDir}, empty);
+        assertCompilationRootsByConfiguration(AbstractSourceGeneratorMojo.CompileRoots.test, srcDir, testDir,
+            empty, new File[]{srcDir, testDir});
+        assertCompilationRootsByConfiguration(AbstractSourceGeneratorMojo.CompileRoots.all, srcDir, testDir,
+            new File[]{srcDir}, new File[]{testDir});
+        assertCompilationRootsByConfiguration(AbstractSourceGeneratorMojo.CompileRoots.none, srcDir, testDir,
+            empty, empty);
+    }
+
+    private void assertCompilationRootsByConfiguration(AbstractSourceGeneratorMojo.CompileRoots compileRoots,
+                                                       File srcDir, File testDir,
+                                                       File[] expectedSource, File[] expectedTest) throws Exception {
+        AbstractSourceGeneratorMojo mojo = createGeneratorMojo();
+        Assume.assumeThat("Ignored because createGeneratorMojo is not implemented", mojo, notNullValue());
+        mojo.generatedSrcDir = srcDir;
+        mojo.generatedTestDir = testDir;
+        mojo.addCompileSourceRoots = compileRoots;
+        mojo.execute();
+
+        assertCompileSourceRoots(mojo.project::getCompileSourceRoots, expectedSource);
+        assertCompileSourceRoots(mojo.project::getTestCompileSourceRoots, expectedTest);
+    }
+
+    private void assertCompileSourceRoots(Supplier<List<String>> roots, File... expectedSources) {
+        List<String> compileSourceRoots = roots.get();
+        Assert.assertThat(compileSourceRoots.size(), is(expectedSources.length));
+        Assert.assertThat(compileSourceRoots, hasItems(
+            Stream.of(expectedSources).map(File::getAbsolutePath).toArray(String[]::new))
+        );
+
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/ApiComponentGeneratorMojoTest.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/ApiComponentGeneratorMojoTest.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/ApiComponentGeneratorMojoTest.java
index 52cf975..ef020e0 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/ApiComponentGeneratorMojoTest.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/ApiComponentGeneratorMojoTest.java
@@ -21,9 +21,14 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.camel.component.test.TestProxy;
+import org.apache.camel.util.CastUtils;
 import org.apache.velocity.VelocityContext;
+import org.junit.Assert;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.is;
+
 /**
  * Tests {@link ApiComponentGeneratorMojo}
  */
@@ -37,6 +42,15 @@ public class ApiComponentGeneratorMojoTest extends AbstractGeneratorMojoTest {
         // delete target files to begin
         collectionFile.delete();
 
+        final ApiComponentGeneratorMojo mojo = createGeneratorMojo();
+
+        mojo.execute();
+
+        // check target file was generated
+        assertExists(collectionFile);
+    }
+
+    protected ApiComponentGeneratorMojo createGeneratorMojo() {
         final ApiComponentGeneratorMojo mojo = new ApiComponentGeneratorMojo();
         configureSourceGeneratorMojo(mojo);
 
@@ -72,10 +86,8 @@ public class ApiComponentGeneratorMojoTest extends AbstractGeneratorMojoTest {
         fromJavadoc.setExcludePackages(JavadocApiMethodGeneratorMojo.DEFAULT_EXCLUDE_PACKAGES);
         fromJavadoc.setExcludeMethods("clone|Current|internal|icache");
         mojo.apis[1].setFromJavadoc(fromJavadoc);
-
-        mojo.execute();
-
-        // check target file was generated
-        assertExists(collectionFile);
+        return mojo;
     }
+    
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
index 3988bb3..7f12785 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
@@ -16,14 +16,14 @@
  */
 package org.apache.camel.maven;
 
-import java.io.File;
-import java.io.IOException;
-
 import org.apache.camel.component.test.TestProxy;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.junit.Test;
 
+import java.io.File;
+import java.io.IOException;
+
 /**
  * Tests {@link FileApiMethodGeneratorMojo}
  */
@@ -38,6 +38,14 @@ public class FileApiMethodGeneratorMojoTest extends AbstractGeneratorMojoTest {
             outFile.delete();
         }
 
+        final FileApiMethodGeneratorMojo mojo = createGeneratorMojo();
+        mojo.execute();
+
+        // check target file was generated
+        assertExists(outFile);
+    }
+
+    protected FileApiMethodGeneratorMojo createGeneratorMojo() {
         final FileApiMethodGeneratorMojo mojo = new FileApiMethodGeneratorMojo();
         mojo.substitutions = new Substitution[2];
         mojo.substitutions[0] = new Substitution(".+", "(.+)", "java.util.List", "$1List", false);
@@ -52,11 +60,7 @@ public class FileApiMethodGeneratorMojoTest extends AbstractGeneratorMojoTest {
         // exclude name2, and int times
         mojo.excludeConfigNames = "name2";
         mojo.excludeConfigTypes = "int";
-
-        mojo.execute();
-
-        // check target file was generated
-        assertExists(outFile);
+        return mojo;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b1943195/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
index 6e34298..71c3959 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
@@ -38,6 +38,15 @@ public class JavadocApiMethodGeneratorMojoTest extends AbstractGeneratorMojoTest
             outFile.delete();
         }
 
+        final JavadocApiMethodGeneratorMojo mojo = createGeneratorMojo();
+        mojo.execute();
+
+        // check target file was generated
+        assertExists(outFile);
+    }
+
+    @Override
+    protected JavadocApiMethodGeneratorMojo createGeneratorMojo() {
         final JavadocApiMethodGeneratorMojo mojo = new JavadocApiMethodGeneratorMojo();
 
         configureSourceGeneratorMojo(mojo);
@@ -52,11 +61,6 @@ public class JavadocApiMethodGeneratorMojoTest extends AbstractGeneratorMojoTest
         mojo.excludePackages = JavadocApiMethodGeneratorMojo.DEFAULT_EXCLUDE_PACKAGES;
         mojo.includeMethods = ".+";
         mojo.excludeMethods = "clone|Current|internal|icache";
-
-        mojo.execute();
-
-        // check target file was generated
-        assertExists(outFile);
+        return mojo;
     }
-
 }