This is an automated email from the ASF dual-hosted git repository. rfscholte pushed a commit to branch MSCRIPTING-8 in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git
commit 63d8d3e398d8c58511840a651e7efca488d6a757 Author: rfscholte <rfscho...@apache.org> AuthorDate: Mon Dec 6 20:56:23 2021 +0100 [MSCRIPTING-8] Support scriptResource --- pom.xml | 56 ++++++--- src/it/mrm/repository/_mscripting-8.jar | Bin 0 -> 867 bytes .../repository/mscripting-8.jar/script.groovy} | 41 ++++--- .../pom.xml => mrm/repository/mscripting-8.pom} | 80 +++++-------- src/it/{ => mrm}/settings.xml | 27 ++++- .../{ => projects}/groovy-script-file-name/pom.xml | 0 .../groovy-script-file-name/test.groov | 0 src/it/{ => projects}/groovy-script-file/pom.xml | 0 .../{ => projects}/groovy-script-file/test.groovy | 0 .../groovy-script-resource}/pom.xml | 107 ++++++++--------- src/it/{ => projects}/groovy-script/pom.xml | 0 .../apache/maven/plugins/scripting/EvalMojo.java | 13 ++- .../plugins/scripting/ResourceScriptEvaluator.java | 127 +++++++++++++++++++++ src/site/markdown/usage.md.vm | 29 ++++- 14 files changed, 331 insertions(+), 149 deletions(-) diff --git a/pom.xml b/pom.xml index 13fb174..1b90110 100644 --- a/pom.xml +++ b/pom.xml @@ -121,25 +121,47 @@ under the License. <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 Binary files /dev/null and b/src/it/mrm/repository/_mscripting-8.jar differ diff --git a/src/it/groovy-script-file/test.groovy b/src/it/mrm/repository/mscripting-8.jar/script.groovy similarity index 92% copy from src/it/groovy-script-file/test.groovy copy to src/it/mrm/repository/mscripting-8.jar/script.groovy index 94d935c..a77c5f8 100644 --- a/src/it/groovy-script-file/test.groovy +++ b/src/it/mrm/repository/mscripting-8.jar/script.groovy @@ -1,21 +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. - */ - - -(1..10).sum() + ' ' + project.artifactId \ No newline at end of file +/* + * 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/groovy-script-file-name/pom.xml b/src/it/mrm/repository/mscripting-8.pom similarity index 58% copy from src/it/groovy-script-file-name/pom.xml copy to src/it/mrm/repository/mscripting-8.pom index a5fdfca..dcf6b38 100644 --- a/src/it/groovy-script-file-name/pom.xml +++ b/src/it/mrm/repository/mscripting-8.pom @@ -1,51 +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>groovy-script</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> - <engineName>groovy</engineName> - <scriptFile>test.groov</scriptFile> - </configuration> - <dependencies> - <!-- ScriptEngines --> - <dependency> - <groupId>org.codehaus.groovy</groupId> - <artifactId>groovy-jsr223</artifactId> - <version>2.4.7</version> - </dependency> - </dependencies> - </plugin> - </plugins> - </build> -</project> +<?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 64% 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 @@ specific language governing permissions and limitations 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 @@ under the License. <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% copy from src/it/groovy-script-file-name/pom.xml copy 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/groovy-script-file-name/pom.xml b/src/it/projects/groovy-script-resource/pom.xml similarity index 83% rename from src/it/groovy-script-file-name/pom.xml rename to src/it/projects/groovy-script-resource/pom.xml index a5fdfca..7c4942b 100644 --- a/src/it/groovy-script-file-name/pom.xml +++ b/src/it/projects/groovy-script-resource/pom.xml @@ -1,51 +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</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> - <engineName>groovy</engineName> - <scriptFile>test.groov</scriptFile> - </configuration> - <dependencies> - <!-- ScriptEngines --> - <dependency> - <groupId>org.codehaus.groovy</groupId> - <artifactId>groovy-jsr223</artifactId> - <version>2.4.7</version> - </dependency> - </dependencies> - </plugin> - </plugins> - </build> -</project> +<?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 @@ public class EvalMojo */ @Parameter private File scriptFile; + + @Parameter String scriptResource; // script variables @Parameter( defaultValue = "${project}", readonly = true ) @@ -100,17 +102,22 @@ public class EvalMojo 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..13d537f --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java @@ -0,0 +1,127 @@ +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 file. Use the engine name to override the engine if the file 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 scriptFile 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 @@ Use the scriptFile-tag to refer to your script. The engine is based on the file- <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