You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/06/15 08:23:00 UTC

tomee git commit: TOMEE-1842 better impl of stripVersion

Repository: tomee
Updated Branches:
  refs/heads/master 54f66e00f -> b2c00f037


TOMEE-1842 better impl of stripVersion


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/b2c00f03
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/b2c00f03
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/b2c00f03

Branch: refs/heads/master
Commit: b2c00f037eaa3c9e79d40ae9bf4035594c733c45
Parents: 54f66e0
Author: Romain manni-Bucau <rm...@gmail.com>
Authored: Wed Jun 15 10:22:47 2016 +0200
Committer: Romain manni-Bucau <rm...@gmail.com>
Committed: Wed Jun 15 10:22:47 2016 +0200

----------------------------------------------------------------------
 .../openejb/maven/plugin/AbstractTomEEMojo.java | 78 +++++++++++---------
 .../maven/plugin/test/PersistJavaagentTest.java |  4 +-
 .../maven/plugin/test/StripVersionTest.java     | 78 ++++++++++++++++++++
 3 files changed, 123 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/b2c00f03/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 b911f43..4a16c2c 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
@@ -531,7 +531,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
                         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);
+                                return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type).resolved;
                             } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                                 throw new IllegalArgumentException(e);
                             }
@@ -539,7 +539,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
                         @Override
                         public File resolve(final String group, final String artifact, final String version) {
                             try {
-                                return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar");
+                                return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar").resolved;
                             } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                                 throw new IllegalArgumentException(e);
                             }
@@ -547,7 +547,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
                         @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);
+                                return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type).resolved;
                             } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                                 throw new IllegalArgumentException(e);
                             }
@@ -759,38 +759,20 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
             }
         } else {
             try {
-                final File file = mvnToFile(lib, defaultType);
+                final FileWithMavenMeta file = mvnToFile(lib, defaultType);
                 if (extractedName == null && (stripVersion || isWar && stripWarVersion)) {
-                    String currentName = file.getName();
-                    currentName = currentName.endsWith("." + defaultType) ?
-                            currentName.substring(0, currentName.length() - defaultType.length() - 1) : currentName;
-                    currentName = currentName.endsWith("-SNAPSHOT") ?
-                            currentName.substring(0, currentName.length() - "-SNAPSHOT".length()) : currentName;
-
-                    int idx = currentName.length() - 1;
-                    while (idx >= 0) {
-                        if (currentName.charAt(idx) == '-') {
-                            break;
-                        }
-                        idx--;
-                    }
-                    if (idx > 0) {
-                        extractedName = currentName.substring(0, idx);
-                        if (!isExplodedWar) { // works for libs
-                            extractedName += "." + defaultType;
-                        }
-                    }
+                    extractedName = file.stripVersion(!isExplodedWar);
                 }
 
-                if (!unzip && !isExplodedWar) {
+                if (!unzip) {
                     final File dest;
                     if (extractedName == null) {
-                        dest = new File(destParent, file.getName());
+                        dest = new File(destParent, file.resolved.getName());
                     } else {
                         dest = new File(destParent, extractedName);
                     }
 
-                    is = new BufferedInputStream(new FileInputStream(file));
+                    is = new BufferedInputStream(new FileInputStream(file.resolved));
                     os = new BufferedOutputStream(new FileOutputStream(dest));
                     copy(is, os);
 
@@ -798,9 +780,9 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
                 } else {
                     if (isExplodedWar) {
                         destParent = Files.mkdirs(new File(rawDestParent, extractedName != null ?
-                                extractedName : file.getName().replace(".war", "")));
+                                extractedName : file.resolved.getName().replace(".war", "")));
                     }
-                    Zips.unzip(file, destParent, !isExplodedWar);
+                    Zips.unzip(file.resolved, destParent, !isExplodedWar);
 
                     getLog().info("Unzipped '" + lib + "' in '" + destParent.getAbsolutePath());
                 }
@@ -814,7 +796,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
         }
     }
 
-    private File mvnToFile(final String lib, final String defaultType) throws ArtifactResolutionException, ArtifactNotFoundException {
+    private FileWithMavenMeta mvnToFile(final String lib, final String defaultType) throws ArtifactResolutionException, ArtifactNotFoundException {
         final String[] infos = lib.split(":");
         final String classifier;
         final String type;
@@ -835,10 +817,10 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
         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 {
+    private FileWithMavenMeta 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();
+        return new FileWithMavenMeta(group, artifact, version, classifier, type, dependencyArtifact.getFile());
     }
 
     private void copyWar() {
@@ -1276,15 +1258,18 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
             String path = javaagent;
             if (!new File(javaagent).isFile()) {
                 try {
-                    final File jar = mvnToFile(javaagent, "jar");
+                    final FileWithMavenMeta jar = mvnToFile(javaagent, "jar");
                     if (persistJavaagents) {
                         final File javaagentFolder = new File(catalinaBase, "javaagent");
                         Files.mkdirs(javaagentFolder);
-                        final String name = jar.getName();
+                        String name = jar.resolved.getName();
+                        if (stripVersion) {
+                            name = jar.stripVersion(true);
+                        }
                         path = "$CATALINA_HOME/javaagent/" + name;
-                        IO.copy(jar, new File(javaagentFolder, name));
+                        IO.copy(jar.resolved, new File(javaagentFolder, name));
                     }
-                    strings.add("-javaagent:" + jar.getAbsolutePath() + args);
+                    strings.add("-javaagent:" + jar.resolved.getAbsolutePath() + args);
                 } catch (final Exception e) {
                     getLog().warn("Can't find " + javaagent);
                     strings.add("-javaagent:" + javaagent + args);
@@ -1605,4 +1590,27 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
         File resolve(String group, String artifact, String version, String type);
         File resolve(String group, String artifact, String version);
     }
+
+    private static class FileWithMavenMeta {
+        private final String group;
+        private final String artifact;
+        private final String version;
+        private final String classifier;
+        private final String type;
+        private final File resolved;
+
+        private FileWithMavenMeta(final String group, final String artifact, final String version,
+                                 final String classifier, final String type, final File resolved) {
+            this.group = group;
+            this.artifact = artifact;
+            this.version = version;
+            this.classifier = classifier;
+            this.type = type;
+            this.resolved = resolved;
+        }
+
+        String stripVersion(final boolean keepExtension) {
+            return artifact + (classifier != null && !classifier.isEmpty() ? "-" + classifier : "") +  "." + type;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/b2c00f03/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/PersistJavaagentTest.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/PersistJavaagentTest.java b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/PersistJavaagentTest.java
index 76b4569..9cc0473 100644
--- a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/PersistJavaagentTest.java
+++ b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/PersistJavaagentTest.java
@@ -28,7 +28,6 @@ import java.util.List;
 
 import static java.util.Arrays.asList;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 public class PersistJavaagentTest {
@@ -40,6 +39,7 @@ public class PersistJavaagentTest {
 
     @Config
     private final boolean persistJavaagents = true;
+
     @Config
     private final File catalinaBase = new File("target/tomee-agent");
 
@@ -51,7 +51,7 @@ public class PersistJavaagentTest {
         assertEquals(1, new File(catalinaBase, "javaagent").listFiles(new FilenameFilter() {
             @Override
             public boolean accept(final File dir, final String name) {
-                return name.startsWith("sirona-") && name.endsWith("-shaded.jar");
+                return name.equals("sirona-javaagent-0.2-incubating-shaded.jar");
             }
         }).length);
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/b2c00f03/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/StripVersionTest.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/StripVersionTest.java b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/StripVersionTest.java
new file mode 100644
index 0000000..8be39c4
--- /dev/null
+++ b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/StripVersionTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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 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.io.FilenameFilter;
+import java.util.List;
+
+import static java.util.Collections.singletonList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class StripVersionTest {
+    @Rule
+    public TomEEMavenPluginRule TMPRule = new TomEEMavenPluginRule();
+
+    @Config
+    private final List<String> javaagents = singletonList("org.apache.sirona:sirona-javaagent:0.2-incubating:jar:shaded");
+
+    @Config
+    private final List<String> libs = singletonList("org.apache.tomee:log4j2-tomee:7.0.0" /*use release to avoid nasty deps*/);
+
+    @Config
+    private final List<String> webapps = singletonList("org.apache.tomee:tomee-webaccess:7.0.0" /*use release to avoid nasty deps*/);
+
+    @Config // otherwise they are not copied
+    private final boolean persistJavaagents = true;
+
+    @Config
+    private boolean stripVersion = true;
+
+    @Config
+    private final File catalinaBase = new File("target/tomee-stripversion");
+
+    @Test
+    public void sironaIsInstalledAndPersisted() throws Exception {
+        assertTrue(catalinaBase.exists());
+
+        assertEquals(1, new File(catalinaBase, "javaagent").listFiles(new FilenameFilter() {
+            @Override
+            public boolean accept(final File dir, final String name) {
+                return name.equals("sirona-javaagent-shaded.jar");
+            }
+        }).length);
+        assertEquals(1, new File(catalinaBase, "lib").listFiles(new FilenameFilter() {
+            @Override
+            public boolean accept(final File dir, final String name) {
+                return  name.equals("log4j2-tomee.jar");
+            }
+        }).length);
+        assertEquals(1, new File(catalinaBase, "webapps").listFiles(new FilenameFilter() {
+            @Override
+            public boolean accept(final File dir, final String name) {
+                return name.equals("tomee-webaccess.war");
+            }
+        }).length);
+
+    }
+}