[MSCRIPTING-8] Support scriptResource
diff --git a/pom.xml b/pom.xml
index 13fb174..1b90110 100644
--- a/pom.xml
+++ b/pom.xml
@@ -121,25 +121,47 @@
<profile>
<id>run-its</id>
<build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-invoker-plugin</artifactId>
- <configuration>
- <projectsDirectory>src/it</projectsDirectory>
- <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
- <preBuildHookScript>setup</preBuildHookScript>
- <postBuildHookScript>verify</postBuildHookScript>
- <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
- <settingsFile>src/it/settings.xml</settingsFile>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-invoker-plugin</artifactId>
+ <configuration>
+ <projectsDirectory>src/it/projects</projectsDirectory>
+ <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
+ <preBuildHookScript>setup</preBuildHookScript>
+ <postBuildHookScript>verify</postBuildHookScript>
+ <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
+ <settingsFile>src/it/mrm/settings.xml</settingsFile>
+ <filterProperties>
+ <mrm.repository.url>${mrm.repository.url}</mrm.repository.url>
+ </filterProperties>
+ <goals>
+ <goal>scripting:eval</goal>
+ </goals>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>mrm-maven-plugin</artifactId>
+ <version>1.3.0</version>
+ <executions>
+ <execution>
<goals>
- <goal>scripting:eval</goal>
+ <goal>start</goal>
+ <goal>stop</goal>
</goals>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
+ </execution>
+ </executions>
+ <configuration>
+ <repositories>
+ <mockRepo>
+ <source>src/it/mrm/repository</source>
+ </mockRepo>
+ <proxyRepo/>
+ </repositories>
+ </configuration>
+ </plugin>
+ </plugins>
</build>
</profile>
</profiles>
diff --git a/src/it/mrm/repository/_mscripting-8.jar b/src/it/mrm/repository/_mscripting-8.jar
new file mode 100644
index 0000000..4bc8711
--- /dev/null
+++ b/src/it/mrm/repository/_mscripting-8.jar
Binary files differ
diff --git a/src/it/mrm/repository/mscripting-8.jar/script.groovy b/src/it/mrm/repository/mscripting-8.jar/script.groovy
new file mode 100644
index 0000000..a77c5f8
--- /dev/null
+++ b/src/it/mrm/repository/mscripting-8.jar/script.groovy
@@ -0,0 +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
+ *
+ * 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.
+ */
+
+assert true
\ No newline at end of file
diff --git a/src/it/mrm/repository/mscripting-8.pom b/src/it/mrm/repository/mscripting-8.pom
new file mode 100644
index 0000000..dcf6b38
--- /dev/null
+++ b/src/it/mrm/repository/mscripting-8.pom
@@ -0,0 +1,29 @@
+<?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>
+
+ <groupId>org.apache.maven.plugins.scripting.its</groupId>
+ <artifactId>mscripting-8</artifactId>
+ <version>1.0.0</version>
+
+</project>
diff --git a/src/it/settings.xml b/src/it/mrm/settings.xml
similarity index 66%
rename from src/it/settings.xml
rename to src/it/mrm/settings.xml
index 35dbcec..7292e58 100644
--- a/src/it/settings.xml
+++ b/src/it/mrm/settings.xml
@@ -19,31 +19,47 @@
under the License.
-->
-<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">
+<settings>
+ <mirrors>
+ <mirror>
+ <id>mrm-maven-plugin</id>
+ <name>Mock Repository Manager</name>
+ <url>@mrm.repository.url@</url>
+ <mirrorOf>*</mirrorOf>
+ </mirror>
+ </mirrors>
<profiles>
<profile>
<id>it-repo</id>
<repositories>
<repository>
- <id>local.central</id>
- <url>@localRepositoryUrl@</url>
+ <id>snapshots</id>
+ <url>@mrm.repository.url@</url>
<releases>
<enabled>true</enabled>
+ <checksumPolicy>ignore</checksumPolicy>
+ <updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
+ <checksumPolicy>ignore</checksumPolicy>
+ <updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
- <id>local.central</id>
- <url>@localRepositoryUrl@</url>
+ <id>snapshots</id>
+ <url>@mrm.repository.url@</url>
<releases>
<enabled>true</enabled>
+ <checksumPolicy>ignore</checksumPolicy>
+ <updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
+ <checksumPolicy>ignore</checksumPolicy>
+ <updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
@@ -52,4 +68,5 @@
<activeProfiles>
<activeProfile>it-repo</activeProfile>
</activeProfiles>
+
</settings>
diff --git a/src/it/groovy-script-file-name/pom.xml b/src/it/projects/groovy-script-file-name/pom.xml
similarity index 100%
rename from src/it/groovy-script-file-name/pom.xml
rename to src/it/projects/groovy-script-file-name/pom.xml
diff --git a/src/it/groovy-script-file-name/test.groov b/src/it/projects/groovy-script-file-name/test.groov
similarity index 100%
rename from src/it/groovy-script-file-name/test.groov
rename to src/it/projects/groovy-script-file-name/test.groov
diff --git a/src/it/groovy-script-file/pom.xml b/src/it/projects/groovy-script-file/pom.xml
similarity index 100%
rename from src/it/groovy-script-file/pom.xml
rename to src/it/projects/groovy-script-file/pom.xml
diff --git a/src/it/groovy-script-file/test.groovy b/src/it/projects/groovy-script-file/test.groovy
similarity index 100%
rename from src/it/groovy-script-file/test.groovy
rename to src/it/projects/groovy-script-file/test.groovy
diff --git a/src/it/projects/groovy-script-resource/pom.xml b/src/it/projects/groovy-script-resource/pom.xml
new file mode 100644
index 0000000..7c4942b
--- /dev/null
+++ b/src/it/projects/groovy-script-resource/pom.xml
@@ -0,0 +1,56 @@
+<?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>
+
+ <groupId>org.apache.maven.plugins.scripting.its</groupId>
+ <artifactId>groovy-script-resource</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>pom</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-scripting-plugin</artifactId>
+ <version>@project.version@</version>
+ <configuration>
+ <scriptResource>script.groovy</scriptResource>
+ </configuration>
+ <dependencies>
+ <!-- Shared Resource -->
+ <dependency>
+ <groupId>org.apache.maven.plugins.scripting.its</groupId>
+ <artifactId>mscripting-8</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ <!-- ScriptEngines -->
+ <dependency>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-jsr223</artifactId>
+ <version>2.4.7</version>
+ </dependency>
+ </dependencies>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/src/it/groovy-script/pom.xml b/src/it/projects/groovy-script/pom.xml
similarity index 100%
rename from src/it/groovy-script/pom.xml
rename to src/it/projects/groovy-script/pom.xml
diff --git a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
index 4a6dbbc..3976f13 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
@@ -59,6 +59,8 @@
*/
@Parameter
private File scriptFile;
+
+ @Parameter String scriptResource;
// script variables
@Parameter( defaultValue = "${project}", readonly = true )
@@ -100,17 +102,22 @@
if ( scriptFile != null )
{
- execute = new FileScriptEvaluator( engineName, scriptFile );
+ execute = new FileScriptEvaluator( engineName, scriptFile );
+
+ }
+ else if ( scriptResource != null )
+ {
+ execute = new ResourceScriptEvaluator( engineName, scriptResource );
}
else if ( script != null )
{
- execute = new StringScriptEvaluator( engineName, script );
+ execute = new StringScriptEvaluator( engineName, script );
}
else
{
- throw new IllegalArgumentException( "Missing script or scriptFile provided" );
+ throw new IllegalArgumentException( "Missing script or scriptFile provided" );
}
return execute;
}
diff --git a/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java b/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java
new file mode 100644
index 0000000..5321226
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java
@@ -0,0 +1,128 @@
+package org.apache.maven.plugins.scripting;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UncheckedIOException;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+/**
+ * Evaluates a script held in a resource. Use the engine name to override the engine if the resource name does not
+ * refer/decode to a valid engine name or not define any at all.
+ *
+ * @author Robert Scholte
+ */
+public class ResourceScriptEvaluator extends AbstractScriptEvaluator
+{
+
+ /**
+ * Not null, existing readable file with the script
+ */
+ private final String resourceName;
+
+ /**
+ * Possibly null engine name
+ */
+ private final String engineName;
+
+ /**
+ * @param engineName optional engine name, used to override the engine selection from the file extension
+ * @param resourceName not null
+ */
+ public ResourceScriptEvaluator( String engineName, String resourceName )
+ {
+ this.resourceName = resourceName;
+
+ this.engineName = engineName;
+ }
+
+ /**
+ * @param engine the script engine.
+ * @param context the script context.
+ * @return the result of the scriptFile.
+ * @throws ScriptException if an error occurs in script.
+ * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext)
+ */
+ protected Object eval( ScriptEngine engine, ScriptContext context ) throws ScriptException
+ {
+
+ try ( InputStream is = this.getClass().getClassLoader().getResourceAsStream( resourceName );
+ Reader reader = new InputStreamReader( is ) )
+ {
+ return engine.eval( reader, context );
+ }
+ catch ( IOException ex )
+ {
+ throw new UncheckedIOException( resourceName + " caused:", ex );
+ }
+ }
+
+ /**
+ * Gets the script engine by engineName, otherwise by extension of the sciptFile
+ *
+ * @param manager the script engine manager
+ * @throws UnsupportedScriptEngineException if specified engine is not available
+ * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#getEngine(javax.script.ScriptEngineManager)
+ */
+ protected ScriptEngine getEngine( ScriptEngineManager manager ) throws UnsupportedScriptEngineException
+ {
+ ScriptEngine result;
+
+ if ( engineName != null && !engineName.isEmpty() )
+ {
+ result = manager.getEngineByName( engineName );
+
+ if ( result == null )
+ {
+ throw new UnsupportedScriptEngineException( "No engine found by name \"" + engineName + "\n" );
+ }
+ }
+ else
+ {
+ String name = resourceName;
+ int fileSepIndex = name.lastIndexOf( '/' );
+ if ( fileSepIndex >= 0 )
+ {
+ name = name.substring( fileSepIndex + 1 );
+ }
+
+ String extension = name;
+ int position = name.indexOf( "." );
+ if ( position >= 0 )
+ {
+ extension = name.substring( position + 1 );
+ }
+ result = manager.getEngineByExtension( extension );
+
+ if ( result == null )
+ {
+ throw new UnsupportedScriptEngineException( "No engine found by extension \"" + extension + "\n" );
+ }
+ }
+ return result;
+ }
+}
diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm
index 809bc10..60cbdb1 100644
--- a/src/site/markdown/usage.md.vm
+++ b/src/site/markdown/usage.md.vm
@@ -69,4 +69,31 @@
<version>2.4.7</version>
</dependency>
</dependencies>
- </plugin>
\ No newline at end of file
+ </plugin>
+
+<h3>Script from File</h3>
+
+Use the scriptResource-tag to refer to your script and include its artifact as a dependency to the plugin. The engine is based on the resource-extension, but can be specified using the engineName-tag.
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-scripting-plugin</artifactId>
+ <version>${project.version}</version>
+ <configuration>
+ <scriptResource>/com/foo/bar/script.groovy</scriptFile>
+ </configuration>
+ <dependencies>
+ <!-- Shared Resource -->
+ <dependency>
+ <groupId/>
+ <artifactId/>
+ <version/>
+ </dependency>
+ <!-- ScriptEngines -->
+ <dependency>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-jsr223</artifactId>
+ <version>2.4.7</version>
+ </dependency>
+ </dependencies>
+ </plugin>
\ No newline at end of file