Initial commit maven-scipting-plugin, a jsr223 wrapping maven plugin.
authorRobert Scholte <rfscholte@apache.org>
Sun, 20 Nov 2016 13:08:33 +0000 (13:08 +0000)
committerRobert Scholte <rfscholte@apache.org>
Sun, 20 Nov 2016 13:08:33 +0000 (13:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1770555 13f79535-47bb-0310-9956-ffa450edef68

pom.xml [new file with mode: 0644]
src/it/groovy-script/pom.xml [new file with mode: 0644]
src/it/settings.xml [new file with mode: 0644]
src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..2af17e3
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,144 @@
+<?xml version='1.0' encoding='UTF-8'?>\r
+\r
+<!--\r
+Licensed to the Apache Software Foundation (ASF) under one\r
+or more contributor license agreements.  See the NOTICE file\r
+distributed with this work for additional information\r
+regarding copyright ownership.  The ASF licenses this file\r
+to you under the Apache License, Version 2.0 (the\r
+"License"); you may not use this file except in compliance\r
+with the License.  You may obtain a copy of the License at\r
+\r
+  http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing,\r
+software distributed under the License is distributed on an\r
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+KIND, either express or implied.  See the License for the\r
+specific language governing permissions and limitations\r
+under the License.\r
+-->\r
+\r
+<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">\r
+  <modelVersion>4.0.0</modelVersion>\r
+\r
+  <parent>\r
+    <groupId>org.apache.maven.plugins</groupId>\r
+    <artifactId>maven-plugins</artifactId>\r
+    <version>30</version>\r
+    <relativePath>../../pom/maven/maven-plugins/pom.xml</relativePath>\r
+  </parent>\r
+\r
+  <artifactId>maven-scripting-plugin</artifactId>\r
+  <version>3.0.0-SNAPSHOT</version>\r
+  <packaging>maven-plugin</packaging>\r
+\r
+  <name>Apache Maven Scripting Plugin</name>\r
+  <description>\r
+    The Maven Scripting Plugin is a plugin that wrapped the Scripting API accoring to JSR223\r
+  </description>\r
+  <inceptionYear>2016</inceptionYear>\r
+\r
+  <prerequisites>\r
+    <maven>${mavenVersion}</maven>\r
+  </prerequisites>\r
+\r
+  <scm>\r
+    <connection>scm:svn:http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-scripting-plugin</connection>\r
+    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-scripting-plugin</developerConnection>\r
+    <url>http://svn.apache.org/viewvc/maven/plugins/trunk/maven-scripting-plugin</url>\r
+  </scm>\r
+  <issueManagement>\r
+    <system>JIRA</system>\r
+    <url>https://issues.apache.org/jira/browse/MSCRIPTING</url>\r
+  </issueManagement>\r
+  <distributionManagement>\r
+    <site>\r
+      <id>apache.website</id>\r
+      <url>scm:svn:https://svn.apache.org/repos/infra/websites/production/maven/components/${maven.site.path}</url>\r
+    </site>\r
+  </distributionManagement>\r
+\r
+  <properties>\r
+    <mavenVersion>3.0</mavenVersion>\r
+  </properties>\r
+\r
+  <dependencies>\r
+    <dependency>\r
+      <groupId>org.apache.maven</groupId>\r
+      <artifactId>maven-plugin-api</artifactId>\r
+      <version>${mavenVersion}</version>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>org.apache.maven.shared</groupId>\r
+      <artifactId>maven-shared-utils</artifactId>\r
+      <version>3.1.0</version>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>org.apache.maven</groupId>\r
+      <artifactId>maven-core</artifactId>\r
+      <version>${mavenVersion}</version>\r
+    </dependency>\r
+\r
+    <!-- dependencies to annotations -->\r
+    <dependency>\r
+      <groupId>org.apache.maven.plugin-tools</groupId>\r
+      <artifactId>maven-plugin-annotations</artifactId>\r
+      <scope>provided</scope>\r
+    </dependency>\r
+\r
+    <!-- ScriptEngines -->\r
+    <dependency>\r
+      <groupId>org.codehaus.groovy</groupId>\r
+      <artifactId>groovy-jsr223</artifactId>\r
+      <version>2.4.7</version>\r
+    </dependency>\r
+\r
+    <!-- Test -->\r
+    <dependency>\r
+      <groupId>junit</groupId>\r
+      <artifactId>junit</artifactId>\r
+      <version>4.12</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>org.apache.maven.plugin-testing</groupId>\r
+      <artifactId>maven-plugin-testing-harness</artifactId>\r
+      <version>2.1</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>org.apache.maven</groupId>\r
+      <artifactId>maven-compat</artifactId>\r
+      <version>${mavenVersion}</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
+  </dependencies>\r
+\r
+  <profiles>\r
+    <profile>\r
+      <id>run-its</id>\r
+      <build>\r
+        <pluginManagement>\r
+          <plugins>\r
+            <plugin>\r
+              <groupId>org.apache.maven.plugins</groupId>\r
+              <artifactId>maven-invoker-plugin</artifactId>\r
+              <configuration>\r
+                <projectsDirectory>src/it</projectsDirectory>\r
+                <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>\r
+                <preBuildHookScript>setup</preBuildHookScript>\r
+                <postBuildHookScript>verify</postBuildHookScript>\r
+                <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>\r
+                <settingsFile>src/it/settings.xml</settingsFile>\r
+                <goals>\r
+                  <goal>scripting:eval</goal>\r
+                </goals>\r
+              </configuration>\r
+            </plugin>\r
+          </plugins>\r
+        </pluginManagement>\r
+      </build>\r
+    </profile>\r
+  </profiles>\r
+</project>\r
diff --git a/src/it/groovy-script/pom.xml b/src/it/groovy-script/pom.xml
new file mode 100644 (file)
index 0000000..5098a79
--- /dev/null
@@ -0,0 +1,47 @@
+<?xml version='1.0' encoding='UTF-8'?>\r
+\r
+<!--\r
+Licensed to the Apache Software Foundation (ASF) under one\r
+or more contributor license agreements.  See the NOTICE file\r
+distributed with this work for additional information\r
+regarding copyright ownership.  The ASF licenses this file\r
+to you under the Apache License, Version 2.0 (the\r
+"License"); you may not use this file except in compliance\r
+with the License.  You may obtain a copy of the License at\r
+\r
+  http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing,\r
+software distributed under the License is distributed on an\r
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+KIND, either express or implied.  See the License for the\r
+specific language governing permissions and limitations\r
+under the License.\r
+-->\r
+\r
+<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">\r
+  <modelVersion>4.0.0</modelVersion>\r
+\r
+  <groupId>org.apache.maven.plugins.scripting.its</groupId>\r
+  <artifactId>groovy-script</artifactId>\r
+  <version>1.0.0-SNAPSHOT</version>\r
+  <packaging>pom</packaging>\r
+  \r
+  <build>\r
+    <plugins>\r
+      <plugin>\r
+        <groupId>org.apache.maven.plugins</groupId>\r
+        <artifactId>maven-scripting-plugin</artifactId>\r
+        <version>@project.version@</version>\r
+        <configuration>\r
+          <engineName>groovy</engineName>\r
+          <script>\r
+          <![CDATA[\r
+            (1..10).sum() + ' ' + project.artifactId\r
+          ]]>\r
+          </script>\r
+        </configuration>\r
+      </plugin>\r
+    </plugins>\r
+  </build>\r
+</project>\r
diff --git a/src/it/settings.xml b/src/it/settings.xml
new file mode 100644 (file)
index 0000000..35dbcec
--- /dev/null
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+\r
+<!--\r
+Licensed to the Apache Software Foundation (ASF) under one\r
+or more contributor license agreements.  See the NOTICE file\r
+distributed with this work for additional information\r
+regarding copyright ownership.  The ASF licenses this file\r
+to you under the Apache License, Version 2.0 (the\r
+"License"); you may not use this file except in compliance\r
+with the License.  You may obtain a copy of the License at\r
+\r
+  http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing,\r
+software distributed under the License is distributed on an\r
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+KIND, either express or implied.  See the License for the\r
+specific language governing permissions and limitations\r
+under the License.\r
+-->\r
+\r
+<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">\r
+  <profiles>\r
+    <profile>\r
+      <id>it-repo</id>\r
+      <repositories>\r
+        <repository>\r
+          <id>local.central</id>\r
+          <url>@localRepositoryUrl@</url>\r
+          <releases>\r
+            <enabled>true</enabled>\r
+          </releases>\r
+          <snapshots>\r
+            <enabled>true</enabled>\r
+          </snapshots>\r
+        </repository>\r
+      </repositories>\r
+      <pluginRepositories>\r
+        <pluginRepository>\r
+          <id>local.central</id>\r
+          <url>@localRepositoryUrl@</url>\r
+          <releases>\r
+            <enabled>true</enabled>\r
+          </releases>\r
+          <snapshots>\r
+            <enabled>true</enabled>\r
+          </snapshots>\r
+        </pluginRepository>\r
+      </pluginRepositories>\r
+    </profile>\r
+  </profiles>\r
+  <activeProfiles>\r
+    <activeProfile>it-repo</activeProfile>\r
+  </activeProfiles>\r
+</settings>\r
diff --git a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
new file mode 100644 (file)
index 0000000..f4c349a
--- /dev/null
@@ -0,0 +1,109 @@
+package org.apache.maven.plugins.scripting;\r
+\r
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  You may obtain a copy of the License at\r
+ *\r
+ *   http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+\r
+import javax.script.ScriptContext;\r
+import javax.script.ScriptEngine;\r
+import javax.script.ScriptEngineManager;\r
+import javax.script.ScriptException;\r
+\r
+import org.apache.maven.plugin.AbstractMojo;\r
+import org.apache.maven.plugin.MojoExecutionException;\r
+import org.apache.maven.plugin.MojoFailureException;\r
+import org.apache.maven.plugins.annotations.Mojo;\r
+import org.apache.maven.plugins.annotations.Parameter;\r
+import org.apache.maven.project.MavenProject;\r
+\r
+/**\r
+ * Evaluate the specified script\r
+ * \r
+ * @author Robert Scholte\r
+ * @since 3.0.0\r
+ */\r
+@Mojo( name = "eval" )\r
+public class EvalMojo\r
+    extends AbstractMojo\r
+{\r
+    @Parameter( required = true )\r
+    private String engineName; // or map extension to engineName??\r
+\r
+    /**\r
+     * When used, also specify the engineName\r
+     */\r
+    @Parameter\r
+    private String script;\r
+\r
+    // script variables\r
+    @Parameter( defaultValue = "${project}", readonly = true )\r
+    private MavenProject project;\r
+    \r
+    private ScriptEngineManager manager = new ScriptEngineManager();\r
+\r
+    @Override\r
+    public void execute()\r
+        throws MojoExecutionException, MojoFailureException\r
+    {\r
+        ScriptEngine engine = null;\r
+        \r
+        if ( script != null )\r
+        {\r
+            engine = getScriptEngine( engineName );\r
+\r
+            if ( engine == null )\r
+            {\r
+                throw new MojoFailureException( "Missing scriptEngine" );\r
+            }\r
+        }\r
+        else\r
+        {\r
+            // from file\r
+        }\r
+\r
+        try\r
+        {\r
+            ScriptContext context = engine.getContext();\r
+            context.setAttribute( "project", project, ScriptContext.GLOBAL_SCOPE );\r
+            \r
+            Object result = engine.eval( script );\r
+            \r
+            getLog().info( "Result:" );\r
+            if ( result != null )\r
+            {\r
+                getLog().info( result.toString() );\r
+            }\r
+        }\r
+        catch ( ScriptException e )\r
+        {\r
+            throw new MojoExecutionException( e.getMessage(), e );\r
+        }\r
+    }\r
+    \r
+    private ScriptEngine getScriptEngine( String name )\r
+    {\r
+        if ( name == null ) \r
+        {\r
+            return null;\r
+        }\r
+        else\r
+        {\r
+            return manager.getEngineByName( engineName );\r
+        }\r
+    }\r
+}\r