Author: kmenard
Date: Tue Oct 3 19:47:04 2006
New Revision: 452701
URL: http://svn.apache.org/viewvc?view=rev&rev=452701
Log:
Initial work on CAY-591: Add a class generator plugin for Maven 2.
Added:
incubator/cayenne/main/trunk/other/maven-plugin/
incubator/cayenne/main/trunk/other/maven-plugin/pom.xml
incubator/cayenne/main/trunk/other/maven-plugin/src/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
Modified:
incubator/cayenne/main/trunk/other/pom.xml
Added: incubator/cayenne/main/trunk/other/maven-plugin/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/other/maven-plugin/pom.xml?view=auto&rev=452701
==============================================================================
--- incubator/cayenne/main/trunk/other/maven-plugin/pom.xml (added)
+++ incubator/cayenne/main/trunk/other/maven-plugin/pom.xml Tue Oct 3 19:47:04
2006
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright 2006 The Apache Software Foundation
+
+ Licensed 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/maven-v4_0_0.xsd">
+ <parent>
+ <artifactId>cayenne-other-parent</artifactId>
+ <groupId>org.apache.cayenne.other</groupId>
+ <version>3.0-incubating-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.cayenne.plugins</groupId>
+ <artifactId>cayenne-maven-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+ <name>Cayenne Maven Plugin</name>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact</artifactId>
+ <version>2.0.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-tools-api</artifactId>
+ <version>2.0.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cayenne.core</groupId>
+ <artifactId>cayenne-jdk1.4</artifactId>
+ <version>${version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cayenne.modeler</groupId>
+ <artifactId>cayenne-modeler</artifactId>
+ <version>${version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>foundrylogic.vpp</groupId>
+ <artifactId>vpp</artifactId>
+ <scope>compile</scope>
+ </dependency>
+
+ <!-- TODO: (KJM 10/03/2006) This should be a transitive dependency of
vpp. -->
+ <dependency>
+ <groupId>ant</groupId>
+ <artifactId>ant</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+</project>
Added:
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java?view=auto&rev=452701
==============================================================================
---
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
(added)
+++
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
Tue Oct 3 19:47:04 2006
@@ -0,0 +1,326 @@
+/*****************************************************************
+ * 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.cayenne.tools;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.MapLoader;
+import org.apache.cayenne.gen.DefaultClassGenerator;
+import org.apache.cayenne.util.Util;
+import org.xml.sax.InputSource;
+
+import java.io.File;
+import java.util.*;
+
+/**
+ * Maven mojo to perform class generation from data map. This class is an
Maven adapter to
+ * DefaultClassGenerator class.
+ *
+ * @author Andrus Adamchik, Kevin Menard
+ * @since 3.0
+ *
+ * @phase generate-sources
+ * @goal cgen
+ */
+public class CayenneGeneratorMojo extends AbstractMojo
+{
+ /**
+ * Project instance used to add generated source code to the build.
+ *
+ * @parameter default-value="${project}"
+ * @required
+ * @readonly
+ */
+ private MavenProject project;
+
+ /**
+ * Path to additional DataMap XML files to use for class generation.
+ *
+ * @parameter expression="${cgen.additionalMaps}"
+ */
+ private File additionalMaps;
+
+ /**
+ * Whether we are generating classes for the client tier in a
+ * Remote Object Persistence application. Default is <code>false</code>.
+ *
+ * @parameter expression="${cgen.client}" default-value="false"
+ */
+ private boolean client;
+
+ /**
+ * Destination directory for Java classes (ignoring their package
names).
+ *
+ * @parameter expression="${cgen.destDir}"
default-value="${project.build.sourceDirectory}/java/generated-sources/cayenne"
+ */
+ private File destDir;
+
+ /**
+ * Specify generated file encoding if different from the default on
current platform.
+ * Target encoding must be supported by the JVM running Maven build.
Standard encodings
+ * supported by Java on all platforms are US-ASCII, ISO-8859-1, UTF-8,
UTF-16BE,
+ * UTF-16LE, UTF-16. See Sun Java Docs for java.nio.charset.Charset for
more information.
+ *
+ * @parameter expression="${cgen.encoding}"
+ */
+ private String encoding;
+
+ /**
+ * Entities (expressed as a perl5 regex) to exclude from template
+ * generation. (Default is to include all entities in the DataMap).
+ *
+ * @parameter expression="${cgen.excludeEntitiesPattern}"
+ */
+ private String excludeEntitiesPattern;
+
+ /**
+ * Entities (expressed as a perl5 regex) to include in template
+ * generation. (Default is to include all entities in the DataMap).
+ *
+ * @parameter expression="${cgen.includeEntitiesPattern}"
+ */
+ private String includeEntitiesPattern;
+
+ /**
+ * If set to <code>true</code>, will generate subclass/superclass pairs,
+ * with all generated code included in superclass (default is
<code>true</code>).
+ *
+ * @parameter expression="${cgen.makePairs}" default-value="true"
+ */
+ private boolean makePairs;
+
+ /**
+ * DataMap XML file to use as a base for class generation.
+ *
+ * @parameter expression="${cgen.map}"
+ * @required
+ */
+ private File map;
+
+ /**
+ * Specifies generator iteration target. "entity" performs one
iteration for each selected entity.
+ * "datamap" performs one iteration per datamap (This is always
one iteration since cgen
+ * currently supports specifying one-and-only-one datamap). (Default is
"entity")
+ *
+ * @parameter expression="${cgen.mode}" default-value="entity"
+ */
+ private String mode;
+
+ /**
+ * Name of file for generated output. (Default is "*.java")
+ *
+ * @parameter expression="${cgen.outputPattern}" default-value="*.java"
+ */
+ private String outputPattern;
+
+ /**
+ * If set to <code>true</code>, will overwrite older versions of
generated classes.
+ * Ignored unless makepairs is set to <code>false</code>.
+ *
+ * @parameter expression="${cgen.overwrite}" default-value="false"
+ */
+ private boolean overwrite;
+
+ /**
+ * Java package name of generated superclasses. Ignored unless
<code>makepairs</code>
+ * set to <code>true</code>. If omitted, each superclass will be assigned
the same
+ * package as subclass. Note that having superclass in a different package
would only
+ * make sense when <code>usepkgpath</code> is set to <code>true</code>.
Otherwise
+ * classes from different packages will end up in the same directory.
+ *
+ * @parameter expression="${cgen.superPkg}"
+ */
+ private String superPkg;
+
+ /**
+ * Location of Velocity template file for Java superclass generation.
Ignored unless
+ * <code>makepairs</code> set to <code>true</code>. If omitted, default
template is used.
+ *
+ * @parameter expression="${cgen.superTemplate}"
+ */
+ private String superTemplate;
+
+ /**
+ * Location of Velocity template file for Java class generation. If
omitted, default template is used.
+ *
+ * @parameter expression="${cgen.template}"
+ */
+ private String template;
+
+ /**
+ * If set to <code>true</code> (default), a directory tree will be
generated in "destDir"
+ * corresponding to the class package structure, if set to
<code>false</code>, classes will
+ * be generated in "destDir" ignoring their package.
+ *
+ * @parameter expression="${cgen.usePkgPath}" default-value="true"
+ */
+ private boolean usePkgPath;
+
+ /**
+ * Specifies template location and generator behavior. "1.1" is
the old behavior,
+ * with templates located in "dotemplates" and
"classgen" as the only
+ * velocity context attribute. "1.2" is the new behavior, with
templates located
+ * in "dotemplates/v1.2" and "objEntity",
"entityUtils",
+ * "stringUtils", and "importUtils" in the velocity
context. (Default is "1.2".)
+ *
+ * @parameter expression="${cgen.version}" default-value="1.1"
+ */
+ private String version;
+
+ private DefaultClassGenerator generator;
+
+ public void execute() throws MojoExecutionException, MojoFailureException
+ {
+ // Create the destination directory if necessary.
+ if (!destDir.exists())
+ {
+ destDir.mkdirs();
+ }
+
+ generator = createGenerator();
+
+ try {
+ processMap();
+ }
+ catch (Throwable th) {
+ th = Util.unwindException(th);
+
+ String thMessage = th.getLocalizedMessage();
+ String message = "Error generating classes: ";
+ message += (!Util.isEmptyString(thMessage)) ? thMessage : th
+ .getClass()
+ .getName();
+
+ this.getLog().error(message);
+
+ throw new MojoExecutionException(message, th);
+ }
+ }
+
+
+ protected void processMap() throws Exception {
+
+ DataMap dataMap = loadDataMap();
+ DataMap additionalDataMaps[] = loadAdditionalDataMaps();
+
+ // Create MappingNamespace for maps.
+ EntityResolver entityResolver = new
EntityResolver(Collections.singleton(dataMap));
+ dataMap.setNamespace(entityResolver);
+ for (int i = 0; i < additionalDataMaps.length; i++) {
+ entityResolver.addDataMap(additionalDataMaps[i]);
+ additionalDataMaps[i].setNamespace(entityResolver);
+ }
+
+ Collection allEntities = dataMap.getObjEntities();
+ List filteredEntities = new ArrayList(allEntities.size());
+
+ // filter client entities
+ if (generator.isClient()) {
+ if (dataMap.isClientSupported()) {
+ Iterator it = allEntities.iterator();
+ while (it.hasNext()) {
+ ObjEntity entity = (ObjEntity) it.next();
+ if (entity.isClientAllowed()) {
+ filteredEntities.add(entity);
+ }
+ }
+ }
+ } else {
+ filteredEntities.addAll(allEntities);
+ }
+
+ // filter names according to the specified pattern
+ NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
+ // TODO: (KJM 10/03/2006) NamePatternMatcher should be updated
to not depend on Ant.
+ null,
+ includeEntitiesPattern,
+ excludeEntitiesPattern);
+ namePatternMatcher.filter(filteredEntities);
+
+ generator.setTimestamp(map.lastModified());
+ generator.setDataMap(dataMap);
+ generator.setObjEntities(filteredEntities);
+ generator.validateAttributes();
+ generator.execute();
+ }
+
+ /**
+ * Loads and returns a DataMap by File.
+ */
+ protected DataMap loadDataMap(File mapName) throws Exception {
+ InputSource in = new InputSource(mapName.getCanonicalPath());
+ return new MapLoader().loadDataMap(in);
+ }
+
+ /**
+ * Loads and returns DataMap based on <code>map</code> attribute.
+ */
+ protected DataMap loadDataMap() throws Exception {
+ return loadDataMap(map);
+ }
+
+ /**
+ * Loads and returns DataMap based on <code>map</code> attribute.
+ */
+ protected DataMap[] loadAdditionalDataMaps() throws Exception {
+ if (null == additionalMaps) {
+ return new DataMap[0];
+ }
+
+ if (!additionalMaps.isDirectory()) {
+ throw new MojoFailureException("'additionalMaps' must be a
directory containing only datamap files.");
+ }
+
+ String[] maps = additionalMaps.list();
+ DataMap dataMaps[] = new DataMap[maps.length];
+ for (int i = 0; i < maps.length; i++) {
+ dataMaps[i] = loadDataMap(new File(maps[i]));
+ }
+ return dataMaps;
+ }
+
+ /**
+ * Factory method to create internal class generator. Called from
constructor.
+ */
+ protected DefaultClassGenerator createGenerator() {
+ DefaultClassGenerator gen = new DefaultClassGenerator();
+
+ gen.setClient(client);
+ gen.setDestDir(destDir);
+ gen.setEncoding(encoding);
+ gen.setMakePairs(makePairs);
+ gen.setMode(mode);
+ gen.setOutputPattern(outputPattern);
+ gen.setOverwrite(overwrite);
+ gen.setSuperPkg(superPkg);
+ gen.setSuperTemplate(superTemplate);
+ gen.setTemplate(template);
+ gen.setUsePkgPath(usePkgPath);
+ gen.setVersionString(version);
+
+ return gen;
+ }
+}
Modified: incubator/cayenne/main/trunk/other/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/other/pom.xml?view=diff&rev=452701&r1=452700&r2=452701
==============================================================================
--- incubator/cayenne/main/trunk/other/pom.xml (original)
+++ incubator/cayenne/main/trunk/other/pom.xml Tue Oct 3 19:47:04 2006
@@ -37,6 +37,7 @@
<module>cayenne-regression-profiler</module>
<module>cayenne-build-tools</module>
<module>confluence-maven-plugin</module>
+ <module>maven-plugin</module>
</modules>
</project>