You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2013/01/04 02:13:32 UTC

svn commit: r1428694 - in /uima/sandbox/uimafit/trunk: ./ uimafit-maven-plugin/ uimafit-maven-plugin/src/ uimafit-maven-plugin/src/main/ uimafit-maven-plugin/src/main/java/ uimafit-maven-plugin/src/main/java/org/ uimafit-maven-plugin/src/main/java/org/...

Author: rec
Date: Fri Jan  4 01:13:31 2013
New Revision: 1428694

URL: http://svn.apache.org/viewvc?rev=1428694&view=rev
Log:
[UIMA-2553] Maven plugin to generate component descriptors from uimaFIT annotations at build time

Added:
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml   (with props)
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java   (with props)
    uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/package-info.java   (with props)
Modified:
    uima/sandbox/uimafit/trunk/pom.xml

Modified: uima/sandbox/uimafit/trunk/pom.xml
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/pom.xml?rev=1428694&r1=1428693&r2=1428694&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/pom.xml (original)
+++ uima/sandbox/uimafit/trunk/pom.xml Fri Jan  4 01:13:31 2013
@@ -532,5 +532,6 @@
 		<module>uimafit</module>
 		<module>uimafit-examples</module>
 		<module>uimafit-spring</module>
+		<module>uimafit-maven-plugin</module>
 	</modules>
 </project>
\ No newline at end of file

Added: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml?rev=1428694&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml (added)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml Fri Jan  4 01:13:31 2013
@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+	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
+
+	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">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>org.apache.uima</groupId>
+		<artifactId>uimafit-parent</artifactId>
+		<version>1.5.0-SNAPSHOT</version>
+	</parent>
+	<artifactId>uimafit-maven-plugin</artifactId>
+	<name>uimaFIT Maven Plugin</name>
+	
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.apache.maven</groupId>
+			<artifactId>maven-plugin-api</artifactId>
+			<version>3.0.4</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.maven</groupId>
+			<artifactId>maven-project</artifactId>
+			<version>2.2.1</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.maven</groupId>
+			<artifactId>maven-artifact</artifactId>
+			<version>2.0.10</version>
+		</dependency>
+		<dependency>
+			<groupId>commons-io</groupId>
+			<artifactId>commons-io</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.uima</groupId>
+			<artifactId>uimafit</artifactId>
+			<version>1.5.0-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.maven.plugin-tools</groupId>
+			<artifactId>maven-plugin-annotations</artifactId>
+			<version>3.2</version>
+			<scope>provided</scope>
+		</dependency>
+	</dependencies>
+	
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-plugin-plugin</artifactId>
+				<version>3.2</version>
+				<configuration>
+					<goalPrefix>descgen-maven-plugin</goalPrefix>
+				</configuration>
+				<executions>
+					<execution>
+						<id>generated-helpmojo</id>
+						<goals>
+							<goal>helpmojo</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+		<pluginManagement>
+			<plugins>
+				<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
+				<plugin>
+					<groupId>org.eclipse.m2e</groupId>
+					<artifactId>lifecycle-mapping</artifactId>
+					<version>1.0.0</version>
+					<configuration>
+						<lifecycleMappingMetadata>
+							<pluginExecutions>
+								<pluginExecution>
+									<pluginExecutionFilter>
+										<groupId>org.apache.maven.plugins</groupId>
+										<artifactId>maven-plugin-plugin</artifactId>
+										<versionRange>[3.2,)</versionRange>
+										<goals>
+											<goal>helpmojo</goal>
+										</goals>
+									</pluginExecutionFilter>
+									<action>
+										<ignore></ignore>
+									</action>
+								</pluginExecution>
+							</pluginExecutions>
+						</lifecycleMappingMetadata>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+	</build>
+</project>
\ No newline at end of file

Propchange: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java?rev=1428694&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java Fri Jan  4 01:13:31 2013
@@ -0,0 +1,180 @@
+/*
+ * 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
+ * 
+ * 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.
+ */
+package org.apache.uima.fit.maven;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+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.ResolutionScope;
+import org.apache.maven.project.MavenProject;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.factory.CollectionReaderFactory;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceSpecifier;
+import org.codehaus.plexus.util.FileUtils;
+import org.xml.sax.SAXException;
+
+/**
+ * Generate descriptor files for uimaFIT-based UIMA components.
+ * 
+ * @see http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html
+ */
+@Mojo(name = "generate", defaultPhase = LifecyclePhase.PROCESS_CLASSES,
+		requiresDependencyResolution = ResolutionScope.COMPILE,
+		requiresDependencyCollection = ResolutionScope.COMPILE)
+public class GenerateDescriptorsMojo
+	extends AbstractMojo
+{
+	@Component
+	private MavenProject project;
+
+	private ClassLoader componentLoader;
+
+	@Override
+	public void execute()
+		throws MojoExecutionException
+	{
+		String[] files = FileUtils.getFilesFromExtension(project.getBuild().getOutputDirectory(),
+				new String[] { "class" });
+
+		// Create a class loader which covers the classes compiled in the current project and all
+		// dependencies.
+		try {
+			List<URL> urls = new ArrayList<URL>();
+			for (Object object : project.getCompileClasspathElements()) {
+				String path = (String) object;
+				getLog().debug("Classpath entry: " + object);
+				urls.add(new File(path).toURI().toURL());
+			}
+			for (Artifact dep : (Set<Artifact>) project.getDependencyArtifacts()) {
+				getLog().debug("Classpath entry: " + dep.getFile());
+				urls.add(dep.getFile().toURI().toURL());
+			}
+			componentLoader = new URLClassLoader(urls.toArray(new URL[] {}), 
+					getClass().getClassLoader());
+		}
+		catch (Exception e) {
+			throw new MojoExecutionException("Cannot initialize classloader", e);
+		}
+
+		for (String file : files) {
+			String base = file.substring(0, file.length() - 6);
+			String clazzName = base.substring(project.getBuild().getOutputDirectory().length() + 1)
+					.replace("/", ".");
+			try {
+				Class clazz = getClass(clazzName);
+				ResourceSpecifier desc = null;
+				switch (getType(clazz)) {
+				case ANALYSIS_ENGINE:
+					desc = AnalysisEngineFactory.createPrimitiveDescription(clazz);
+					break;
+				case COLLECTION_READER:
+					desc = CollectionReaderFactory.createDescription(clazz);
+				default:
+					// Do nothing
+				}
+
+				if (desc != null) {
+					toXML(desc, base + ".xml");
+				}
+			}
+			catch (SAXException e) {
+				getLog().warn("Cannot serialize descriptor for [" + clazzName + "]", e);
+			}
+			catch (IOException e) {
+				getLog().warn("Cannot write descriptor for [" + clazzName + "]", e);
+			}
+			catch (ClassNotFoundException e) {
+				getLog().warn("Cannot analyze class [" + clazzName + "]", e);
+			}
+			catch (ResourceInitializationException e) {
+				getLog().warn("Cannot generate descriptor for [" + clazzName + "]", e);
+			}
+		}
+	}
+
+	/**
+	 * Save descriptor XML to file system.
+	 * @throws IOException 
+	 * @throws SAXException 
+	 */
+	private void toXML(ResourceSpecifier aDesc, String aFilename) throws SAXException, IOException
+	{
+		OutputStream os = null;
+		try {
+			File out = new File(aFilename);
+			getLog().info("Writing descriptor to: " + out);
+			os = new FileOutputStream(out);
+			aDesc.toXML(os);
+		}
+		finally {
+			IOUtils.closeQuietly(os);
+		}
+	}
+
+	/**
+	 * Load class using the component classloader.
+	 * @throws ClassNotFoundException 
+	 */
+	private Class getClass(String aClassName)
+		throws ClassNotFoundException
+	{
+		return componentLoader.loadClass(aClassName);
+	}
+
+	/**
+	 * Determine what kind of class it is.
+	 * @throws ClassNotFoundException 
+	 */
+	@SuppressWarnings({ "rawtypes", "unchecked" })
+	private ComponentType getType(Class aClass)
+		throws ClassNotFoundException
+	{
+			Class iCR = getClass("org.apache.uima.collection.CollectionReader");
+			Class iAE = getClass("org.apache.uima.analysis_component.AnalysisComponent");
+			if (iCR.isAssignableFrom(aClass)) {
+				return ComponentType.COLLECTION_READER;
+			}
+			else if (iAE.isAssignableFrom(aClass)) {
+				return ComponentType.ANALYSIS_ENGINE;
+			}
+			else {
+				return ComponentType.NONE;
+			}
+	}
+
+	private enum ComponentType
+	{
+		COLLECTION_READER, ANALYSIS_ENGINE, NONE;
+	}
+}

Propchange: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/package-info.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/package-info.java?rev=1428694&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/package-info.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/package-info.java Fri Jan  4 01:13:31 2013
@@ -0,0 +1,22 @@
+/*
+ * 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
+ * 
+ * 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.
+ */
+/**
+ * Maven Mojos for uimaFIT.
+ */
+package org.apache.uima.fit.maven;
\ No newline at end of file

Propchange: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native