Repository: tomee Updated Branches: refs/heads/master 16f874061 -> 697b40673
TOMEE-1690 adding js and groovy customizers to tomee maven plugin Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/697b4067 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/697b4067 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/697b4067 Branch: refs/heads/master Commit: 697b406736d840eaec7bb303145229863055b5d0 Parents: 16f8740 Author: Romain Manni-Bucau <rmannibu...@gmail.com> Authored: Wed Dec 30 12:26:52 2015 +0100 Committer: Romain Manni-Bucau <rmannibu...@gmail.com> Committed: Wed Dec 30 12:26:52 2015 +0100 ---------------------------------------------------------------------- .../openejb/maven/plugin/AbstractTomEEMojo.java | 75 +++++++++++++++++++- .../maven/plugin/test/JsCustomizertest.java | 56 +++++++++++++++ 2 files changed, 128 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/697b4067/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java ---------------------------------------------------------------------- diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java index 62fe7b5..e4cd0ee 100644 --- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java +++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java @@ -49,6 +49,10 @@ import org.apache.tomee.util.QuickServerXmlParser; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.util.FileUtils; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import javax.script.SimpleBindings; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.Closeable; @@ -60,6 +64,7 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.StringReader; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; @@ -235,6 +240,12 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo { @Parameter protected List<String> customizers; + @Parameter + protected List<String> jsCustomizers; + + @Parameter + protected List<String> groovyCustomizers; + @Parameter(defaultValue = "${project}", readonly = true, required = true) protected MavenProject project; @@ -498,6 +509,9 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo { thread.setContextClassLoader(currentLoader); } } + + scriptCustomization(jsCustomizers, "js"); + scriptCustomization(groovyCustomizers, "groovy"); } else { alignConfigOnServerXmlCurrentConfig(); } @@ -505,6 +519,51 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo { run(); } + private void scriptCustomization(final List<String> customizers, final String ext) throws MojoExecutionException { + if (customizers != null) { + final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext); + if (engine == null) { + throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency."); + } + for (final String js : customizers) { + try { + final SimpleBindings bindings = new SimpleBindings(); + bindings.put("catalinaBase", catalinaBase.getAbsolutePath()); + bindings.put("resolver", new Resolver() { + @Override + public File resolve(final String group, final String artifact, final String version, + final String classifier, final String type) { + try { + return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type); + } catch (final ArtifactResolutionException | ArtifactNotFoundException e) { + throw new IllegalArgumentException(e); + } + } + @Override + public File resolve(final String group, final String artifact, final String version) { + try { + return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar"); + } catch (final ArtifactResolutionException | ArtifactNotFoundException e) { + throw new IllegalArgumentException(e); + } + } + @Override + public File resolve(final String group, final String artifact, final String version, final String type) { + try { + return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type); + } catch (final ArtifactResolutionException | ArtifactNotFoundException e) { + throw new IllegalArgumentException(e); + } + } + }); + engine.eval(new StringReader(js), bindings); + } catch (final ScriptException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + } + } + private void alignConfigOnServerXmlCurrentConfig() { final File sXml = new File(catalinaBase, "conf/server.xml"); if (sXml.isFile()) { @@ -776,9 +835,13 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo { classifier = null; } - final Artifact artifact = factory.createDependencyArtifact(infos[0], infos[1], createFromVersion(infos[2]), type, classifier, SCOPE_COMPILE); - resolver.resolve(artifact, remoteRepos, local); - return artifact.getFile(); + return resolve(infos[0], infos[1], infos[2], classifier, type); + } + + private File resolve(final String group, final String artifact, final String version, final String classifier, final String type) throws ArtifactResolutionException, ArtifactNotFoundException { + final Artifact dependencyArtifact = factory.createDependencyArtifact(group, artifact, createFromVersion(version), type, classifier, SCOPE_COMPILE); + resolver.resolve(dependencyArtifact, remoteRepos, local); + return dependencyArtifact.getFile(); } private void copyWar() { @@ -1541,4 +1604,10 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo { } public abstract String getCmd(); + + public interface Resolver { + File resolve(String group, String artifact, String version, String classifier, String type); + File resolve(String group, String artifact, String version, String type); + File resolve(String group, String artifact, String version); + } } http://git-wip-us.apache.org/repos/asf/tomee/blob/697b4067/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java ---------------------------------------------------------------------- diff --git a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java new file mode 100644 index 0000000..b8c340f --- /dev/null +++ b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java @@ -0,0 +1,56 @@ +/* + * 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.openejb.maven.plugin.test; + +import edu.emory.mathcs.backport.java.util.Collections; +import org.apache.openejb.maven.plugin.Config; +import org.apache.openejb.maven.plugin.TomEEMavenPluginRule; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.util.List; +import java.util.zip.ZipFile; + +import static java.util.Collections.singletonList; +import static org.junit.Assert.assertTrue; + +public class JsCustomizertest { + @Rule + public final TomEEMavenPluginRule TMPRule = new TomEEMavenPluginRule().noRun(); + + @Config + private final List<String> jsCustomizers = singletonList( + // copy junit in lib/junit-test.jar + "var File = Java.type('java.io.File');" + + "var Files = Java.type('java.nio.file.Files');" + + "var StandardCopyOption = Java.type('java.nio.file.StandardCopyOption');" + + "" + + "var junit = resolver.resolve('junit', 'junit', '4.12');" + + "Files.copy(junit.toPath(), new File(catalinaBase, 'lib/JsCustomizertest.jar').toPath(), StandardCopyOption.REPLACE_EXISTING);" + ); + + @Config + private final File catalinaBase = new File("target/JsCustomizertest"); + + @Test + public void run() throws Exception { + final File file = new File(catalinaBase, "lib/JsCustomizertest.jar"); + assertTrue(file.isFile()); + assertTrue(Collections.list(new ZipFile(file).entries()).size() > 300); + } +}