Author: rec
Date: Fri May 31 12:00:19 2013
New Revision: 1488182

URL: http://svn.apache.org/r1488182
Log:
[UIMA-2471] add a jcasgen-maven-plugin (and m2e connector)
- Switched to annotation-based Maven component configuration
- Downgraded to Maven 3.0 (was 3.0.4)

Modified:
    uima/uimaj/trunk/jcasgen-maven-plugin/pom.xml
    
uima/uimaj/trunk/jcasgen-maven-plugin/src/main/java/org/apache/uima/tools/jcasgen/maven/JCasGenMojo.java

Modified: uima/uimaj/trunk/jcasgen-maven-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/uima/uimaj/trunk/jcasgen-maven-plugin/pom.xml?rev=1488182&r1=1488181&r2=1488182&view=diff
==============================================================================
--- uima/uimaj/trunk/jcasgen-maven-plugin/pom.xml (original)
+++ uima/uimaj/trunk/jcasgen-maven-plugin/pom.xml Fri May 31 12:00:19 2013
@@ -1,20 +1,20 @@
 <!--
-   Licensed to the Apache Software Foundation (ASF) under one
-   or more contributor license agreements.  See the NOTICE file
-   distributed with this work for additional information
-   regarding copyright ownership.  The ASF licenses this file
-   to you under the Apache License, Version 2.0 (the
-   "License"); you may not use this file except in compliance
-   with the License.  You may obtain a copy of the License at
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements. See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership. The ASF licenses this file
+       to you under the Apache License, Version 2.0 (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, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-   KIND, either express or implied.  See the License for the
-   specific language governing permissions and limitations
-   under the License.
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied. See the License for the
+       specific language governing permissions and limitations
+       under the License.
 -->
 <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";>
@@ -33,15 +33,22 @@
                <relativePath>../uimaj-parent/pom.xml</relativePath>
        </parent>
        <dependencies>
+               <!-- Dependencies on the Maven Plugin API and friends -->
                <dependency>
                        <groupId>org.apache.maven</groupId>
                        <artifactId>maven-plugin-api</artifactId>
-                       <version>3.0.4</version>
+                       <version>3.0</version>
                </dependency>
                <dependency>
                        <groupId>org.apache.maven</groupId>
                        <artifactId>maven-core</artifactId>
-                       <version>3.0.4</version>
+                       <version>3.0</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.apache.uima</groupId>

Modified: 
uima/uimaj/trunk/jcasgen-maven-plugin/src/main/java/org/apache/uima/tools/jcasgen/maven/JCasGenMojo.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/trunk/jcasgen-maven-plugin/src/main/java/org/apache/uima/tools/jcasgen/maven/JCasGenMojo.java?rev=1488182&r1=1488181&r2=1488182&view=diff
==============================================================================
--- 
uima/uimaj/trunk/jcasgen-maven-plugin/src/main/java/org/apache/uima/tools/jcasgen/maven/JCasGenMojo.java
 (original)
+++ 
uima/uimaj/trunk/jcasgen-maven-plugin/src/main/java/org/apache/uima/tools/jcasgen/maven/JCasGenMojo.java
 Fri May 31 12:00:19 2013
@@ -33,6 +33,11 @@ import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.project.MavenProject;
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.resource.ResourceManager;
@@ -52,227 +57,242 @@ import org.sonatype.plexus.build.increme
  * Note that by default this runs at the process-resources phase because it 
requires the XML
  * descriptor files to already be at the appropriate places on the classpath, 
and the
  * generate-resources phase runs before resources are copied.
- * 
- * @goal generate
- * @phase process-resources
- * @requiresDependencyResolution compile
  */
-public class JCasGenMojo extends AbstractMojo {
-
-  /**
-   * The path to the XML type system description.
-   * 
-   * @parameter
-   * @required
-   * @readonly
-   */
-  private String typeSystem;
-
-  /**
-   * The directory where the generated sources will be written.
-   * 
-   * @parameter 
default-value="${project.build.directory}/generated-sources/jcasgen"
-   * @required
-   * @readonly
-   */
-  private File outputDirectory;
-
-  /**
-   * The Maven Project.
-   * 
-   * @parameter property="project"
-   * @required
-   * @readonly
-   */
-  private MavenProject project;
-
-  /**
-   * The Plexus build context, used to see if files have changed.
-   * 
-   * @component
-   */
-  private BuildContext buildContext;
-
-  public void execute() throws MojoExecutionException, MojoFailureException {
-
-    // add the generated sources to the build
-    if (!this.outputDirectory.exists()) {
-      this.outputDirectory.mkdirs();
-      this.buildContext.refresh(this.outputDirectory);
-    }
-    this.project.addCompileSourceRoot(this.outputDirectory.getPath());
-
-    // the type system is relative to the base directory
-    File typeSystemFile = new File(this.project.getBasedir(), this.typeSystem);
+@Mojo(name = "generate", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, 
requiresDependencyResolution = ResolutionScope.COMPILE)
+public class JCasGenMojo
+    extends AbstractMojo
+{
+    @Component
+    private MavenProject project;
+
+    @Component
+    private BuildContext buildContext;
+
+    /**
+     * The path to the XML type system description.
+     */
+    @Parameter(required = true)
+    private String typeSystem;
+
+    /**
+     * The directory where the generated sources will be written.
+     */
+    @Parameter(defaultValue = 
"${project.build.directory}/generated-sources/jcasgen", required = true)
+    private File outputDirectory;
 
-    // assemble the classpath
-    List<String> elements;
-    try {
-      elements = this.project.getCompileClasspathElements();
-    } catch (DependencyResolutionRequiredException e) {
-      throw new MojoExecutionException(e.getMessage(), e);
-    }
-    StringBuilder classpathBuilder = new StringBuilder();
-    for (String element : elements) {
-      if (classpathBuilder.length() > 0) {
-        classpathBuilder.append(File.pathSeparatorChar);
-      }
-      classpathBuilder.append(element);
-    }
-    String classpath = classpathBuilder.toString();
+    @Override
+    public void execute()
+        throws MojoExecutionException, MojoFailureException
+    {
+
+        // add the generated sources to the build
+        if (!this.outputDirectory.exists()) {
+            this.outputDirectory.mkdirs();
+            this.buildContext.refresh(this.outputDirectory);
+        }
+        this.project.addCompileSourceRoot(this.outputDirectory.getPath());
 
-    // skip JCasGen if there are no changes in the type system file or the 
files it references
-    if (!this.buildContext.hasDelta(this.typeSystem) && 
!this.hasDelta(typeSystemFile, classpath)) {
-      this.getLog().info("JCasGen: Skipped, since no type system changes were 
detected");
-      return;
-    }
+        // the type system is relative to the base directory
+        File typeSystemFile = new File(this.project.getBasedir(), 
this.typeSystem);
 
-    // run JCasGen to generate the Java sources
-    Jg jCasGen = new Jg();
-    String[] args = new String[] { "-jcasgeninput", typeSystemFile.toString(), 
"-jcasgenoutput",
-        this.outputDirectory.getAbsolutePath(), "=jcasgenclasspath", classpath 
};
-    try {
-      jCasGen.main0(args, null, new JCasGenProgressMonitor(), new 
JCasGenErrors());
-    } catch (JCasGenException e) {
-      throw new MojoExecutionException(e.getMessage(), e.getCause());
-    }
+        // assemble the classpath
+        List<String> elements;
+        try {
+            elements = this.project.getCompileClasspathElements();
+        }
+        catch (DependencyResolutionRequiredException e) {
+            throw new MojoExecutionException(e.getMessage(), e);
+        }
+        StringBuilder classpathBuilder = new StringBuilder();
+        for (String element : elements) {
+            if (classpathBuilder.length() > 0) {
+                classpathBuilder.append(File.pathSeparatorChar);
+            }
+            classpathBuilder.append(element);
+        }
+        String classpath = classpathBuilder.toString();
 
-    // signal that the output directory has changed
-    this.buildContext.refresh(this.outputDirectory);
-  }
+        // skip JCasGen if there are no changes in the type system file or the 
files it references
+        if (!this.buildContext.hasDelta(this.typeSystem)
+                && !this.hasDelta(typeSystemFile, classpath)) {
+            this.getLog().info("JCasGen: Skipped, since no type system changes 
were detected");
+            return;
+        }
 
-  private class JCasGenProgressMonitor implements IProgressMonitor {
+        // run JCasGen to generate the Java sources
+        Jg jCasGen = new Jg();
+        String[] args = new String[] { "-jcasgeninput", 
typeSystemFile.toString(),
+                "-jcasgenoutput", this.outputDirectory.getAbsolutePath(), 
"=jcasgenclasspath",
+                classpath };
+        try {
+            jCasGen.main0(args, null, new JCasGenProgressMonitor(), new 
JCasGenErrors());
+        }
+        catch (JCasGenException e) {
+            throw new MojoExecutionException(e.getMessage(), e.getCause());
+        }
 
-    public JCasGenProgressMonitor() {
+        // signal that the output directory has changed
+        this.buildContext.refresh(this.outputDirectory);
     }
 
-    @Override
-    public void done() {
-    }
+    private class JCasGenProgressMonitor
+        implements IProgressMonitor
+    {
 
-    @Override
-    public void beginTask(String name, int totalWorked) {
-    }
+        public JCasGenProgressMonitor()
+        {
+        }
 
-    @Override
-    public void subTask(String message) {
-      getLog().info("JCasGen: " + message);
-    }
+        @Override
+        public void done()
+        {
+        }
 
-    @Override
-    public void worked(int work) {
-    }
-  }
+        @Override
+        public void beginTask(String name, int totalWorked)
+        {
+        }
 
-  private class JCasGenErrors implements IError {
+        @Override
+        public void subTask(String message)
+        {
+            getLog().info("JCasGen: " + message);
+        }
 
-    public JCasGenErrors() {
+        @Override
+        public void worked(int work)
+        {
+        }
     }
 
-    @Override
-    public void newError(int severity, String message, Exception exception) {
-      String fullMessage = "JCasGen: " + message;
-      if (severity == IError.INFO) {
-        getLog().info(fullMessage, exception);
-      } else if (severity == IError.WARN) {
-        getLog().warn(fullMessage, exception);
-      } else if (severity == IError.ERROR) {
-        throw new JCasGenException(exception.getMessage(), exception);
-      } else {
-        throw new UnsupportedOperationException("Unknown severity level: " + 
severity);
-      }
-    }
-  }
+    private class JCasGenErrors
+        implements IError
+    {
 
-  private static class JCasGenException extends RuntimeException {
-    private static final long serialVersionUID = 1L;
+        public JCasGenErrors()
+        {
+        }
 
-    public JCasGenException(String message, Throwable cause) {
-      super(message, cause);
+        @Override
+        public void newError(int severity, String message, Exception exception)
+        {
+            String fullMessage = "JCasGen: " + message;
+            if (severity == IError.INFO) {
+                getLog().info(fullMessage, exception);
+            }
+            else if (severity == IError.WARN) {
+                getLog().warn(fullMessage, exception);
+            }
+            else if (severity == IError.ERROR) {
+                throw new JCasGenException(exception.getMessage(), exception);
+            }
+            else {
+                throw new UnsupportedOperationException("Unknown severity 
level: " + severity);
+            }
+        }
     }
-  }
 
-  private boolean hasDelta(File typeSystemFile, String classpath) {
-    // load the type system and resolve the imports using the classpath
-    TypeSystemDescription typeSystemDescription;
-    try {
-      XMLInputSource in = new XMLInputSource(typeSystemFile.toURI().toURL());
-      typeSystemDescription = 
UIMAFramework.getXMLParser().parseTypeSystemDescription(in);
-      ResourceManager resourceManager = 
UIMAFramework.newDefaultResourceManager();
-      resourceManager.setExtensionClassPath(classpath, true);
-      typeSystemDescription.resolveImports(resourceManager);
-      // on any exception, the type system was invalid, so assume no files 
have changed
-    } catch (InvalidXMLException e) {
-      return false;
-    } catch (MalformedURLException e) {
-      return false;
-    } catch (IOException e) {
-      return false;
+    private static class JCasGenException
+        extends RuntimeException
+    {
+        private static final long serialVersionUID = 1L;
+
+        public JCasGenException(String message, Throwable cause)
+        {
+            super(message, cause);
+        }
     }
 
-    File buildOutputDirectory = new 
File(this.project.getBuild().getOutputDirectory());
+    private boolean hasDelta(File typeSystemFile, String classpath)
+    {
+        // load the type system and resolve the imports using the classpath
+        TypeSystemDescription typeSystemDescription;
+        try {
+            XMLInputSource in = new 
XMLInputSource(typeSystemFile.toURI().toURL());
+            typeSystemDescription = 
UIMAFramework.getXMLParser().parseTypeSystemDescription(in);
+            ResourceManager resourceManager = 
UIMAFramework.newDefaultResourceManager();
+            resourceManager.setExtensionClassPath(classpath, true);
+            typeSystemDescription.resolveImports(resourceManager);
+            // on any exception, the type system was invalid, so assume no 
files have changed
+        }
+        catch (InvalidXMLException e) {
+            return false;
+        }
+        catch (MalformedURLException e) {
+            return false;
+        }
+        catch (IOException e) {
+            return false;
+        }
+
+        File buildOutputDirectory = new 
File(this.project.getBuild().getOutputDirectory());
 
-    // map each resource from its target location to its source location
-    Map<File, File> targetToSource = new HashMap<File, File>();
-    for (Resource resource : this.project.getResources()) {
-      File resourceDir = new File(resource.getDirectory());
-      if (resourceDir.exists()) {
-
-        // scan for the resource files
-        List<String> includes = resource.getIncludes();
-        if (includes.isEmpty()) {
-          includes = Arrays.asList("**");
-        }
-        List<String> excludes = resource.getExcludes();
-        DirectoryScanner scanner = new DirectoryScanner();
-        scanner.setBasedir(resourceDir);
-        scanner.setIncludes(includes.toArray(new String[includes.size()]));
-        scanner.setExcludes(excludes.toArray(new String[excludes.size()]));
-        scanner.scan();
-
-        // map each of the resources from its target location to its source 
location
-        String targetPath = resource.getTargetPath();
-        for (String filePath : scanner.getIncludedFiles()) {
-          File sourceFile = new File(resourceDir, filePath);
-          File baseDirectory = targetPath != null ? new 
File(buildOutputDirectory, targetPath)
-                  : buildOutputDirectory;
-          File targetFile = new File(baseDirectory, filePath);
-          targetToSource.put(targetFile, sourceFile);
+        // map each resource from its target location to its source location
+        Map<File, File> targetToSource = new HashMap<File, File>();
+        for (Resource resource : this.project.getResources()) {
+            File resourceDir = new File(resource.getDirectory());
+            if (resourceDir.exists()) {
+
+                // scan for the resource files
+                List<String> includes = resource.getIncludes();
+                if (includes.isEmpty()) {
+                    includes = Arrays.asList("**");
+                }
+                List<String> excludes = resource.getExcludes();
+                DirectoryScanner scanner = new DirectoryScanner();
+                scanner.setBasedir(resourceDir);
+                scanner.setIncludes(includes.toArray(new 
String[includes.size()]));
+                scanner.setExcludes(excludes.toArray(new 
String[excludes.size()]));
+                scanner.scan();
+
+                // map each of the resources from its target location to its 
source location
+                String targetPath = resource.getTargetPath();
+                for (String filePath : scanner.getIncludedFiles()) {
+                    File sourceFile = new File(resourceDir, filePath);
+                    File baseDirectory = targetPath != null ? new 
File(buildOutputDirectory,
+                            targetPath) : buildOutputDirectory;
+                    File targetFile = new File(baseDirectory, filePath);
+                    targetToSource.put(targetFile, sourceFile);
+                }
+            }
         }
-      }
-    }
 
-    // search through the type system description for source files that have 
changed
-    for (TypeDescription type : typeSystemDescription.getTypes()) {
-      URL typeSystemURL = type.getSourceUrl();
-      if (typeSystemURL != null) {
-        File targetFile;
-        try {
-          targetFile = new File(typeSystemURL.toURI());
-          // for any type system source that is not a File, assume it has not 
changed
-        } catch (URISyntaxException e) {
-          continue;
-        } catch (IllegalArgumentException e) {
-          continue;
-        }
-        File sourceFile = targetToSource.get(targetFile);
-        if (sourceFile != null) {
-          // for any type system file that is also a resource file, return 
true if it has changed
-          if (this.buildContext.hasDelta(sourceFile)) {
-            this.getLog().info("Type system file " + sourceFile + " has 
changed");
-            return true;
-          }
-          // for any type system file that is in the same project, return true 
if it has changed
-          if 
(targetFile.getAbsolutePath().startsWith(this.project.getBasedir().getAbsolutePath()))
 {
-            if (this.buildContext.hasDelta(targetFile)) {
-              this.getLog().info("Type system file " + sourceFile + " has 
changed");
-              return true;
+        // search through the type system description for source files that 
have changed
+        for (TypeDescription type : typeSystemDescription.getTypes()) {
+            URL typeSystemURL = type.getSourceUrl();
+            if (typeSystemURL != null) {
+                File targetFile;
+                try {
+                    targetFile = new File(typeSystemURL.toURI());
+                    // for any type system source that is not a File, assume 
it has not changed
+                }
+                catch (URISyntaxException e) {
+                    continue;
+                }
+                catch (IllegalArgumentException e) {
+                    continue;
+                }
+                File sourceFile = targetToSource.get(targetFile);
+                if (sourceFile != null) {
+                    // for any type system file that is also a resource file, 
return true if it has
+                    // changed
+                    if (this.buildContext.hasDelta(sourceFile)) {
+                        this.getLog().info("Type system file " + sourceFile + 
" has changed");
+                        return true;
+                    }
+                    // for any type system file that is in the same project, 
return true if it has
+                    // changed
+                    if (targetFile.getAbsolutePath().startsWith(
+                            this.project.getBasedir().getAbsolutePath())) {
+                        if (this.buildContext.hasDelta(targetFile)) {
+                            this.getLog().info("Type system file " + 
sourceFile + " has changed");
+                            return true;
+                        }
+                    }
+                }
             }
-          }
         }
-      }
+        // no type system files have changed
+        return false;
     }
-    // no type system files have changed
-    return false;
-  }
 }


Reply via email to