Skip to content
Browse files

Initial import into new public SVN repository.

  • Loading branch information...
1 parent 671cbf2 commit c2e60080f6b3f3560dd43aa62284df83dbb14394 @nirvdrum committed
View
7 .classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
View
23 .project
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>maven-jython-plugin</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.maven.ide.eclipse.maven2Builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.maven.ide.eclipse.maven2Nature</nature>
+ </natures>
+</projectDescription>
View
87 pom.xml
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Copyright 2006 - 2007 Servprise International, Inc.
+ ~
+ ~ 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">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.servprise.maven.plugins</groupId>
+ <artifactId>maven-jython-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+ <name>Jython Maven Plugin</name>
+ <version>0.1</version>
+ <url>http://dev.servprise.com/</url>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ <debug>false</debug>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <artifactId>maven-source-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact</artifactId>
+ <version>2.0.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-tools-api</artifactId>
+ <version>2.0.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.python</groupId>
+ <artifactId>jython</artifactId>
+ <version>2.2-beta2</version>
+ </dependency>
+ </dependencies>
+
+ <distributionManagement>
+ <repository>
+ <id>servprise-repository</id>
+ <name>Servprise Repository</name>
+ <url>scp://plato/var/www/servprise.com/dev/maven-repository</url>
+ </repository>
+ </distributionManagement>
+</project>
+
View
27 site/pom.xml
@@ -0,0 +1,27 @@
+<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">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.servprise.maven.plugins</groupId>
+ <artifactId>maven-jython-plugin-site</artifactId>
+ <version>0.1</version>
+ <name>Maven Jython Plugin</name>
+ <url>http://dev.servprise.com/maven-jython-plugin/</url>
+ <inceptionYear>2006</inceptionYear>
+ <distributionManagement>
+ <site>
+ <id>servprise-repository</id>
+ <name>Servprise Repository</name>
+ <url>scp://plato/var/www/servprise.com/dev/maven-jython-plugin</url>
+ </site>
+ </distributionManagement>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-site-plugin</artifactId>
+ <configuration>
+ <locales>en</locales>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
View
120 site/src/site/apt/configuration.apt
@@ -0,0 +1,120 @@
+Plugin Configuration
+
+ The maven-jython-plugin is mostly a fa�ade to the <jythonc> tool. If
+ you are familiar with the configuration items of that tool, then you should
+ pick up the plugin configuration quickly. Of course, since the intention
+ is to improve upon what jythonc provides, there are additional configuration
+ items as well, while others have been removed. As we push forward to a final,
+ stable release, these options may change. Any such change will be noted on the
+ main page to reduce user confusion.
+
+* jythonHome (default: None -- must be provided)
+
+ This element must point to a valid Jython installation. As of this writing, Jython 2.2-beta-1
+ is the preferred release to be used. maven-jython-plugin has only been tested with 2.2-beta-1.
+
+ You can specify the full path to your installation via either a configuration element in your POM:
+
++------------------------------------------+
+<jythonHome>C:\dev\jython2.2b1</jythonHome>
++------------------------------------------+
+
+ or via the command line, with the expression:
+
++---------------------------------------+
+'-Djython.jythonHome=C:\dev\jython2.2b1'
++---------------------------------------+
+
+ Obviously, you need to account for the path for your own Jython installation.
+
+* sourceDir (default: $basedir/src/main/jython)
+
+ By default, the <sourceDir> value is set to <$basedir/src/main/jython>, so that your project's
+ main Jython source files sit alongside their Java brethren. If you are using a non-standard directory
+ layout, then you can override the <sourceDir> value:
+
++-----------------------------------------------------+
+<sourceDir>C:\temp\my_project\jython_files</sourceDir>
++-----------------------------------------------------+
+
+ or via the command line, with the expression:
+
++---------------------------------------------------+
+'-Djython.sourceDir=C:\temp\my_project\jython_files'
++---------------------------------------------------+
+
+* testSourceDir (default: $basedir/test/main/jython)
+
+ By default, the <testSourceDir> value is set to <$basedir/test/main/jython>, so that your project's
+ test Jython source files sit alongside their Java brethren. If you are using a non-standard directory
+ layout, then you can override the <testSourceDir> value:
+
++-------------------------------------------------------------------+
+<testSourceDir>C:\temp\my_project\jython_files\tests</testSourceDir>
++-------------------------------------------------------------------+
+
+ or via the command line, with the expression:
+
++-------------------------------------------------------------+
+'-Djython.testSourceDir=C:\temp\my_project\jython_files\tests'
++-------------------------------------------------------------+
+
+
+* deep (default: false)
+
+ If you are familiar with <jythonc>, you know what this configuration item does. It is a boolean
+ value, so set it to "true" if you wish to deep compile your sources.
+
+ It is extremely likely that this option will be removed in the next release, in favor of a more
+ maven-friendly approach. Please see the {{{faq.html}FAQ}} for more details on how this may proceed.
+ Until then, use this configuration item with caution.
+
++-------------------------------------------------------------------+
+<deep>true</deep>
++-------------------------------------------------------------------+
+
+ or via the command line, with the expression:
+
++-------------------------------------------------------------+
+'-Djython.deep=true'
++-------------------------------------------------------------+
+
+
+* core (default: false)
+
+ If you are familiar with <jythonc>, you know what this configuration item does. It is a boolean
+ value, so set it to "true" if you wish to deep compile your sources.
+
+ It is extremely likely that this option will be removed in the next release, in favor of a more
+ maven-friendly approach. Please see the {{{faq.html}FAQ}} for more details on how this may proceed.
+ Until then, use this configuration item with caution.
+
++-------------------------------------------------------------------+
+<core>true</core>
++-------------------------------------------------------------------+
+
+ or via the command line, with the expression:
+
++-------------------------------------------------------------+
+'-Djython.core=true'
++-------------------------------------------------------------+
+
+
+* all (default: false)
+
+ If you are familiar with <jythonc>, you know what this configuration item does. It is a boolean
+ value, so set it to "true" if you wish to deep compile your sources.
+
+ It is extremely likely that this option will be removed in the next release, in favor of a more
+ maven-friendly approach. Please see the {{{faq.html}FAQ}} for more details on how this may proceed.
+ Until then, use this configuration item with caution.
+
++-------------------------------------------------------------------+
+<all>true</all>
++-------------------------------------------------------------------+
+
+ or via the command line, with the expression:
+
++-------------------------------------------------------------+
+'-Djython.all=true'
++-------------------------------------------------------------+
View
57 site/src/site/apt/faq.apt
@@ -0,0 +1,57 @@
+Frequently Asked Questions
+
+ [[1]] <<Where do I send bugs/patches/complaints/praise/questions/etc?>>
+
+ At some point, this plugin will probably migrate to either the ASF or Codehaus, and at
+ that time, we will use whatever infrastructure and processes they have in place. The
+ only reason it hasn't thus far was because of lack of clarification on this process
+ and because we didn't have time to jump through the necessary hoops (the lead developer
+ of this plugin is an ASF committer, but not a Codehaus member).
+
+ In any event, for the interim, please direct any of the above communication to our
+ {{{mailto:support@servprise.com}support alias}}, where it will be addressed.
+
+ [[2]] <<How do I build a wholly contained application?>>
+
+ Using the maven-dependency-plugin is probably the best way at building a wholly
+ contained Jython application. With this plugin, you can unpack the Jython JAR and
+ bundle it with your own application. This will allow you to distribute a single JAR
+ to your customers, relieving the requirement of having Jython installed locally.
+
+ Note that the maven-dependency-plugin must execute prior to the <package> phase, so that
+ the distribution directory will contain both your class files and the Jython class files.
+ In maven 2.0.x, there is no "pre-package" phase, so you must arbitrarily choose one that will
+ execute before <package>. In the following example, we bind to the <test> phase, since we
+ have no Jython test sources. If you do, you should bind to some other phase occurring after
+ <test>, but before <package>. Please read the "Build Lifecycle Phase Reference" section at
+ {http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html} for further details.
+
++--------------------------------------------------------+
+<project>
+...
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>unpack-dependencies</id>
+ <configuration>
+ <excludeTransitive>true</excludeTransitive>
+ <outputDirectory>
+ ${project.build.outputDirectory}
+ </outputDirectory>
+ </configuration>
+ <phase>test</phase>
+ <goals>
+ <goal>unpack-dependencies</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+...
+</project>
++--------------------------------------------------------+
+
View
41 site/src/site/apt/index.apt
@@ -0,0 +1,41 @@
+ -----
+ Maven Jython Plugin
+ -----
+ Servprise International, Inc.
+ -----
+
+Introduction
+
+ The maven-jython-plugin is a plugin for the {{{http://maven.apache.org/}Maven 2}} project management system that allows {{{http://www.jython.org}Jython}} to be integrated into your projects.
+
+Current Status
+
+ The plugin is admittedly in a very nascent state. It has, however, been used to generate commercial software. We use it to
+ build our open source WebReboot(R) command-line tool. The plugin is available as open source software
+ under the {{{http://www.apache.org/licenses/LICENSE-2.0.html}Apache Software License v2}}. Unfortunately, we do not yet have
+ a publicly accessible Subversion repository, but you can download and view the sources from the {{{http://dev.servprise.com/maven-repository/com/servprise/maven/plugins/maven-jython-plugin/0.1/maven-jython-plugin-0.1-sources.jar}sources jar}}.
+
+ We will update this page as the plugin matures, but if you'd like to keep in touch with the ongoings, you may want to subscribe to our
+ {{{http://dev.servprise.com/weblog/}weblog}}. In the interest of full disclosure, please note that this weblog also contains
+ posts unrelated to this plugin, mostly for Servprise product developments. We do try to make good use of categories so that you
+ can filter out posts as appropriate, however.
+
+Features
+
+ * Easily fire up an interactive Jython shell
+
+ * Compile main Jython source files to Java classes
+
+ * Compile test Jython source files to Java classes
+
+ * {{{inferred-packages.html}Inferred package structure}} to make life simpler
+
+Additional Information
+
+ As the plugin evolves, so will the documentation. The following should be enough to get you started, however:
+
+ {{{usage.html}How to Use the Plugin}}
+
+ {{{configuration.html}How to Configure the Plugin}}
+
+ {{{shell.html}Running the Jython Shell}}
View
29 site/src/site/apt/inferred-packages.apt
@@ -0,0 +1,29 @@
+Inferred Package Structure
+
+ Jython is a great language-level tool for executing Python within a JVM.
+ There is a divide, however, between the Python world and the Java world.
+ Jython generally does a good job bridging between the two, but falls short in a few areas, in our opinion.
+ One such way is in compiling Jython source files to Java classes.
+
+ Jython uses a tool called <jythonc> for Java class generation and compilation.
+ This tool executes without any regard to physical directory structure and by default
+ places all generated Java classes into the default package. For a variety of reasons,
+ not providing packages for Java classes is generally frowned upon. To its credit,
+ <jythonc> does have a flag for providing a package for a generated class, but this
+ package name must be provided -- i.e., it is not inferred from the physical directory
+ structure as is the case for <javac>.
+
+ The maven-jython-plugin attempts to bring <jythonc>'s behavior a little closer to that
+ of <javac>'s. This is certainly not warranted in all cases, but for the use case this
+ plugin addresses (integrating Jython classes in a Java project), it makes sense.
+
+ Just to clarify, if you have the following file:
+
+ <$basedir/src/main/jython/com/example/whatever/Program.py>
+
+ then that file will be compiled as:
+
+ <com.example.whatever.Program>
+
+ This is all done without any additional configuration, so you can build hierarchies of Jython
+ source files and be assured that they will be compiled into an appropriate Java package.
View
20 site/src/site/apt/shell.apt
@@ -0,0 +1,20 @@
+Running the Jython Shell
+
+ As a convenience to using the <jython> command, the maven-jython-plugin has
+ a means of running the Jython shell. This is in further pursuit of allowing
+ a user to get started running Jython immediately, without having to do a full
+ local installation of the project.
+
+ In order to use the Jython shell, your project must be configured to use the
+ maven-jython-plugin. If it is not already, please review the sections on
+ common setup in {{{usage.html}the usage guide}}.
+
+ Once setup, you can start an interactive Jython shell by executing the following
+ from a system shell in your project's directory:
+
++---------------+
+mvn jython:shell
++---------------+
+
+ Maven will fetch any necessary dependencies and when all done, you'll have an
+ interactive Jython shell to play around with.
View
110 site/src/site/apt/usage.apt
@@ -0,0 +1,110 @@
+Plugin Usage
+
+* Common Setup
+
+ In order to use the plugin, you'll need to add some configuration items to your POM.
+
+ First, you'll need to add the Servprise repository:
+
++-----------------------------------------------------------+
+<project>
+...
+ <repositories>
+ <repository>
+ <id>Servprise Repository</id>
+ <url>http://dev.servprise.com/maven-repository</url>
+ </repository>
+ </repositories>
+...
+</project>
++-----------------------------------------------------------+
+
+ Next, you'll need to add the plugin as dependency of your project:
+
++-----------------------------------------------------+
+<project>
+...
+ <dependencies>
+ <dependency>
+ <groupId>com.servprise.maven.plugins</groupId>
+ <artifactId>maven-jython-plugin</artifactId>
+ <version>0.1</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+...
+</project>
++-----------------------------------------------------+
+
+ Finally, you'll want to tie the maven-jython-plugin into your build process. In order to do this, add something similar to the following:
+
+* Building Main Sources
+
+ The POM snippet below illustrates common usage of the plugin to build your
+ project's main Jython files. If you do not use the common directory structure
+ (i.e., <$basedir/src/main/jython/>), then you will have to review the {{{configuration.html}configuration guide}}.
+
++---------------------------------------------------------+
+<project>
+...
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-jython-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <configuration>
+ <jythonHome>C:\dev\jython2.2b1</jythonHome>
+ </configuration>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+...
+</project>
++---------------------------------------------------------+
+
+ Note the the <jythonHome> configuration element is required, implying that you must have Jython installed
+ locally on your system. We hope to relax that requirement in future releases, but that will likely rely on changes
+ to be made in the way Jython is distributed.
+
+
+* Building Test Sources
+
+ Building test sources is identical to building the main sources, except that you execute the <testCompile> goal
+ rather than <compile> goal. Also note that the execution ID should be different as well, in the event that you
+ need to compile both main and test Jython sources.
+
+ The POM snippet below illustrates common usage of the plugin to build your
+ project' test Jython files. If you do not use the common directory structure
+ (i.e., <$basedir/test/main/jython/>), then you will have to review the {{{configuration.html}configuration guide}}.
+
++---------------------------------------------------------+
+<project>
+...
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-jython-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>testCompile</id>
+ <configuration>
+ <jythonHome>C:\dev\jython2.2b1</jythonHome>
+ </configuration>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+...
+</project>
++---------------------------------------------------------+
View
BIN site/src/site/resources/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
35 site/src/site/site.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project name="maven-jython-plugin">
+ <bannerLeft>
+ <name>Servprise</name>
+ <src>images/logo.png</src>
+ <href>http://dev.servprise.com/</href>
+ </bannerLeft>
+ <body>
+ <links>
+ <item name="WebReboot is a registered trademark of Servprise International, Inc. in the United States and/or other countries."/>
+ <item name="Servprise" href="http://www.servprise.com/"/>
+ <item name="Servprise Dev" href="http://dev.servprise.com/"/>
+ <item name="Weblog" href="http://dev.servprise.com/weblog/"/>
+ </links>
+
+ <menu name="Maven Jython Plugin">
+ <item name="Home" href="index.html"/>
+ <item name="Usage" href="usage.html"/>
+ <item name="Configuration" href="configuration.html"/>
+ <item name="Jython Shell" href="shell.html"/>
+ <item name="FAQ" href="faq.html"/>
+ </menu>
+
+ <menu name="Servprise Products">
+ <item name="WebReboot&amp;reg; Enterprise" href="http://www.servprise.com/WebRebootEnterprise.html"/>
+ <item name="WebReboot&amp;reg; 3.0" href="http://www.servprise.com/WebReboot.html"/>
+ </menu>
+
+ <menu name="Servprise Dev">
+ <item name="Community Support (Fora)" href="http://dev.servprise.com/fora/"/>
+ <item name="Email Support" href="mailto:support@servprise.com"/>
+ <item name="Weblog" href="http://dev.servprise.com/weblog/"/>
+ </menu>
+ </body>
+</project>
View
317 src/main/java/com/servprise/plugins/jython/AbstractCompilerMojo.java
@@ -0,0 +1,317 @@
+/**
+ * Copyright 2006 - 2007 Servprise International, Inc.
+ *
+ * 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.
+ */
+
+package com.servprise.plugins.jython;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+/**
+ * Base set of functionality for concrete mojos to compile
+ * jython source files to Java class files.
+ *
+ * @author <a href="mailto:kmenard@servprise.com">Kevin Menard</a>
+ */
+public abstract class AbstractCompilerMojo extends AbstractMojo
+{
+ /**
+ * @parameter default-value="${plugin.artifacts}"
+ * @required
+ * @readonly
+ */
+ private List pluginArftifacts;
+
+ /**
+ * Path to a jython installation.
+ *
+ * @parameter expression="${jython.jythonHome}"
+ * @required
+ */
+ private String jythonHome;
+
+ /**
+ * Compile all Python dependencies of the module. This is used for creating applets.
+ *
+ * @parameter expression="${jython.deep}" default-value="false"
+ */
+ private boolean deep;
+
+ /**
+ * Include the core Jython libraries (about 130K). Needed for applets since Netscape
+ * doesn't yet support multiple archives. Implies <code>deep = true</code>.
+ *
+ * @parameter expression="${jython.core}" default-value="false"
+ */
+ private boolean core;
+
+ /**
+ * Include all of the Jython libraries (everything in core + compiler and parser).
+ * Implies <code>deep = true</code> and <code>core = true</code>.
+ *
+ * @parameter expression="${jython.all}" default-value="false"
+ */
+ private boolean all;
+
+ public abstract File getSourceDir();
+
+ public abstract File getDestDir();
+
+ public void execute() throws MojoExecutionException, MojoFailureException
+ {
+ // Firs thing to do is perform a sanity check on the mojo's parameters.
+ checkValidDirectory("sourceDir", getSourceDir());
+ checkValidDirectory("jythonHome", new File(jythonHome));
+
+ // Cycle through all this plugin's artifacts until we find Jython. Then add it to the classpath.
+ // This is necessary for Jython to work properly, since Maven's use of classworlds rather than classpaths
+ // messes with Jython.
+ for (final Iterator it = pluginArftifacts.iterator(); it.hasNext();)
+ {
+ final Artifact a = (Artifact) it.next();
+
+ // Add the Jython JAR to the classpath so that it may be indexed properly by jythonc.
+ if (a.getArtifactId().equals("jython"))
+ {
+ System.setProperty("java.class.path", System.getProperty("java.class.path") + ";" + a.getFile().getAbsolutePath());
+ }
+ }
+
+ // Since Jython relies on .py files being installed on the local machine, we need a path
+ // to that local installation.
+ System.setProperty("python.home", jythonHome);
+
+
+
+ // Start building up the command and arguments for executing jythonc in its own process.
+ final List<String> partialArgs = new ArrayList<String>();
+
+ // We assume that "java" is on the system path. At some point, however, we should build up a complete path to the binary.
+ partialArgs.add("java");
+
+ // Make sure that the jython jar is on the classpath.
+ partialArgs.add("-cp");
+ partialArgs.add(jythonHome + "/jython.jar");
+
+ // This is the main jython class we will be executing.
+ partialArgs.add("org.python.util.jython");
+
+ // Tell the jython class that we want to execute the jythonc tool.
+ partialArgs.add(jythonHome + "/Tools/jythonc/jythonc.py");
+
+
+
+ // From here on out, we're passing additional arguments that the jythonc.py file expects to see.
+ // org.python.util.jython will make sure that the jythonc.py file executes with the proper set of arguments.
+
+ // Let jythonc know what directory we want to work out of.
+ partialArgs.add("--workdir=" + getDestDir().getAbsolutePath());
+
+ if (true == deep)
+ {
+ partialArgs.add("--deep");
+ }
+
+ if (true == core)
+ {
+ partialArgs.add("--core");
+ }
+
+ if (true == all)
+ {
+ partialArgs.add("--all");
+ }
+
+ final List<String> args = new ArrayList<String>();
+ for (final File file: getJythonFiles(getSourceDir()))
+ {
+ try
+ {
+ args.clear();
+ args.addAll(partialArgs);
+
+ final String path = file.getCanonicalPath();
+ final String packageName = extractPackage(file);
+
+ // If the jython file has an implied package name, tell jythonc to use that
+ // package when generating the Java source file.
+ if (null != packageName)
+ {
+ args.add("--package");
+ args.add(packageName);
+ }
+
+ // Finish off the command by passing the jython source file as the last argument.
+ args.add(path);
+
+ // TODO: (KJM 4/3/07) Compiling each file in its own process is a bit slow, but necessary right now to ensure correctness of package name. At some point, we should probably hash by package name and compile all files in a package at once.
+ // Compile the jython source file.
+ runJythonc(args);
+ }
+ catch (final IOException e)
+ {
+ throw new MojoExecutionException("Failed to retrieve the path to a jython source file.", e);
+ }
+ catch (final Exception e)
+ {
+ throw new MojoExecutionException("Failed to run jythonc.", e);
+ }
+ }
+ }
+
+ /**
+ * Log the debug message.
+ *
+ * @param message The message to log.
+ */
+ private void debug(final String message)
+ {
+ this.getLog().debug(message);
+ }
+
+ /**
+ * Retrieves all the jython source files in the given directory and all its subdirectories.
+ *
+ * @param directory The directory to scan.
+ *
+ * @return A list of all Jython source files contained in the directory.
+ */
+ private List<File> getJythonFiles(final File directory)
+ {
+ final List<File> ret = new ArrayList<File>();
+
+ // Scan the directory for Jython files.
+ for (final File file : directory.listFiles(new SimpleFilenameFilter()))
+ {
+ // If the file is a directory, then recursively scan that directory.
+ if (file.isDirectory())
+ {
+ ret.addAll(getJythonFiles(file));
+ }
+
+ // Otherwise, add the file directory to the returned output.
+ else
+ {
+ ret.add(file);
+ }
+ }
+
+ return ret;
+ }
+
+ /**
+ * Extract the appropriate Java package to use for the Jython source file based
+ * upon its directory structure.
+ *
+ * @param file The file to extract the package for.
+ *
+ * @return The extracted package name or <code>null</code> if none exists.
+ *
+ * @throws IOException On file I/O errors.
+ */
+ private String extractPackage(final File file) throws IOException
+ {
+ String ret = null;
+
+ final String path = file.getCanonicalPath();
+
+ // A file only has a package if "sourceDir" is not the directory in which the file is contained.
+ // In this case, the package is the empty package, represented by null.
+ if (false == file.getParentFile().equals(getSourceDir()))
+ {
+ ret = path.substring(getSourceDir().getCanonicalPath().length() + 1, path.indexOf(file.getName()) - 1);
+ ret = ret.replaceAll("\\" + File.separator, ".");
+ }
+
+ debug("Discovered the package: " + ret);
+
+ return ret;
+ }
+
+ private void runJythonc(final List<String> args) throws IOException, InterruptedException
+ {
+ // Now that we've built up the command we want to run, set things up so it can run in its own process.
+ final StringBuffer cmd = new StringBuffer();
+ for (final String arg : args)
+ {
+ cmd.append(arg).append(" ");
+ }
+ debug("Command executed: " + cmd);
+
+ // Build up the process environment and start the process.
+ final ProcessBuilder pb = new ProcessBuilder(args);
+ pb.redirectErrorStream(true);
+ final Process p = pb.start();
+
+ // Capture all of the output from jythonc and dump it to STDOUT.
+ final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line = "";
+ while (null != line)
+ {
+ System.out.println(line);
+
+ line = in.readLine();
+ }
+
+ // Block until the jythonc process finishes executing.
+ debug("Jythonc status: " + p.waitFor());
+ }
+
+ /**
+ * Utility method to ensure that a supplied parameter is actually a directory.
+ *
+ * @param directoryName The name of the parameter.
+ * @param value The parameter value supplied by the user.
+ *
+ * @throws MojoFailureException If the value is not a valid directory.
+ */
+ private void checkValidDirectory(final String directoryName, final File value) throws MojoFailureException
+ {
+ if ((null == value) || (false == value.isDirectory()) || (false == value.exists()))
+ {
+ throw new MojoFailureException("'" + directoryName + "' is not a valid directory: " + value);
+ }
+ }
+
+ /**
+ * Simple filter so that we only deal with actual Jython source files.
+ *
+ * @author Kevin Menard
+ */
+ private class SimpleFilenameFilter implements FilenameFilter
+ {
+ /**
+ * We accept all files that end with the ".py" extension or that are themselves directories.
+ */
+ public boolean accept(final File dir, final String name)
+ {
+ final File full = new File(dir, name);
+
+ // TODO: (KJM 03/20/07) This should probably be configurable.
+ return name.endsWith(".py") || full.isDirectory();
+ }
+ }
+}
View
58 src/main/java/com/servprise/plugins/jython/CompilerMojo.java
@@ -0,0 +1,58 @@
+/**
+ * Copyright 2006 - 2007 Servprise International, Inc.
+ *
+ * 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.
+ */
+
+package com.servprise.plugins.jython;
+
+import java.io.File;
+
+/**
+ * Maven mojo to compile jython source files to Java class files.
+ *
+ * @author <a href="mailto:kmenard@servprise.com">Kevin Menard</a>
+ *
+ * @phase compile
+ * @goal compile
+ */
+public class CompilerMojo extends AbstractCompilerMojo
+{
+ // TODO: (KJM 12/3/06) Check that the expression mirrors that of the compile mojo.
+ /**
+ * Destination directory for Java classes (ignoring their package names).
+ *
+ * @parameter expression="${jython.sourceDir}" default-value="${project.build.sourceDirectory}/../jython/"
+ */
+ private File sourceDir;
+
+ /**
+ * The directory where compiled test classes go.
+ *
+ * @parameter expression="${project.build.outputDirectory}"
+ * @required
+ * @readonly
+ */
+ private File destDir;
+
+
+ public File getSourceDir()
+ {
+ return sourceDir;
+ }
+
+ public File getDestDir()
+ {
+ return destDir;
+ }
+}
View
38 src/main/java/com/servprise/plugins/jython/JythonShellMojo.java
@@ -0,0 +1,38 @@
+/**
+ * Copyright 2006 Servprise International, Inc.
+ *
+ * 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.
+ */
+
+package com.servprise.plugins.jython;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.python.util.jython;
+
+/**
+ * Maven mojo to start an interactive jython shell.
+ *
+ * @author Kevin Menard
+ *
+ * @prefix jython
+ * @goal shell
+ */
+public class JythonShellMojo extends AbstractMojo
+{
+ public void execute() throws MojoExecutionException, MojoFailureException
+ {
+ jython.main(new String[]{});
+ }
+}
View
59 src/main/java/com/servprise/plugins/jython/TestCompilerMojo.java
@@ -0,0 +1,59 @@
+/**
+ * Copyright 2006 - 2007 Servprise International, Inc.
+ *
+ * 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.
+ */
+
+package com.servprise.plugins.jython;
+
+import java.io.File;
+
+/**
+ * Maven mojo to compile jython test source files to Java test class files.
+ *
+ * @author <a href="mailto:kmenard@servprise.com">Kevin Menard</a>
+ *
+ * @phase test-compile
+ * @goal testCompile
+ */
+public class TestCompilerMojo extends AbstractCompilerMojo
+{
+ // TODO: (KJM 12/3/06) Check that the expression mirrors that of the compile mojo.
+ /**
+ * Destination directory for Java classes (ignoring their package names).
+ *
+ * @parameter expression="${jython.testSourceDir}" default-value="${project.build.testSourceDirectory}/../jython/"
+ */
+ private File sourceDir;
+
+ /**
+ * The directory where compiled test classes go.
+ *
+ * @parameter expression="${project.build.testOutputDirectory}"
+ * @required
+ * @readonly
+ */
+ private File destDir;
+
+
+ public File getSourceDir()
+ {
+ return sourceDir;
+ }
+
+ public File getDestDir()
+ {
+ return destDir;
+ }
+
+}

0 comments on commit c2e6008

Please sign in to comment.
Something went wrong with that request. Please try again.