[maven-debian-helper] 03/03: Split java wrappers for Maven 2 and Maven 3

Jakub Adam xhaakon-guest at moszumanska.debian.org
Sun Jun 8 17:14:29 UTC 2014


This is an automated email from the git hooks/post-receive script.

xhaakon-guest pushed a commit to branch topic-maven3
in repository maven-debian-helper.

commit e168cdc91a76489860d7ff3a878346485494885c
Author: Jakub Adam <jakub.adam at ktknet.cz>
Date:   Fri May 3 19:28:07 2013 +0200

    Split java wrappers for Maven 2 and Maven 3
    
    This is needed because Maven 3 requires newer classworlds with
    changed API. Wrappers now don't import Maven classes, the main()
    method is called with help of reflection and classworlds itself.
---
 debian/control                                     |  1 +
 debian/rules                                       |  2 +-
 etc/maven/m2-debian-nodocs.conf                    |  2 +-
 etc/maven/m2-debian.conf                           |  2 +-
 .../src/main/java/org/debian/maven/Wrapper.java    | 80 ++++++----------------
 .../maven/{Wrapper.java => WrapperBase.java}       | 27 +++-----
 .../main/java/org/debian/maven/WrapperMaven3.java  | 54 +++++++++++++++
 7 files changed, 90 insertions(+), 78 deletions(-)

diff --git a/debian/control b/debian/control
index 816bedb..41a4cb8 100644
--- a/debian/control
+++ b/debian/control
@@ -13,6 +13,7 @@ Build-Depends-Indep: junit4,
                      libcommons-io-java,
                      libmaven-plugin-tools-java,
                      libplexus-velocity-java,
+                     libplexus-classworlds2-java,
                      maven-repo-helper (>= 1.8.6)
 Standards-Version: 3.9.5
 Vcs-Git: git://anonscm.debian.org/pkg-java/maven-debian-helper.git
diff --git a/debian/rules b/debian/rules
index cfcf982..8beec24 100755
--- a/debian/rules
+++ b/debian/rules
@@ -9,7 +9,7 @@ JAVA_HOME            := /usr/lib/jvm/default-java
 PERL_DIR             := $(shell perl -MConfig -e 'print $$Config{vendorlib}')
 DEB_JARS             := ant-junit junit commons-io classworlds maven-core maven-artifact maven-artifact-manager maven-model \
  maven-compat maven-plugin-api maven-project maven-scm-api velocity file-management plexus-utils \
- plexus-container-default-alpha maven-repo-helper
+ plexus-container-default-alpha maven-repo-helper plexus-classworlds2
 DEB_ANT_BUILD_TARGET := package #javadoc
 DEB_ANT_BUILDFILE    := debian/build.xml
 DEB_ANT_ARGS         := -Dpackage=$(PACKAGE) -Dbin.package=$(PACKAGE)
diff --git a/etc/maven/m2-debian-nodocs.conf b/etc/maven/m2-debian-nodocs.conf
index 92cbe58..da67eee 100644
--- a/etc/maven/m2-debian-nodocs.conf
+++ b/etc/maven/m2-debian-nodocs.conf
@@ -1,4 +1,4 @@
-main is org.debian.maven.Wrapper from debian
+main is org.debian.maven.WrapperMaven3 from debian
 
 set maven.home default /usr/share/maven
 
diff --git a/etc/maven/m2-debian.conf b/etc/maven/m2-debian.conf
index c790246..cb0f3ed 100644
--- a/etc/maven/m2-debian.conf
+++ b/etc/maven/m2-debian.conf
@@ -1,4 +1,4 @@
-main is org.debian.maven.Wrapper from debian
+main is org.debian.maven.WrapperMaven3 from debian
 
 set maven.home default /usr/share/maven
 
diff --git a/maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java b/maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java
index 3348850..f58460e 100644
--- a/maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java
+++ b/maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2009 Torsten Werner.
+ * Copyright 2013 Debian Java Developers.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,76 +16,40 @@
 
 package org.debian.maven;
 
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.util.Enumeration;
-import java.util.Properties;
+import java.lang.reflect.Method;
 
-import org.apache.maven.cli.MavenCli;
 import org.codehaus.classworlds.ClassWorld;
+import org.codehaus.classworlds.ClassRealm;
+import org.codehaus.classworlds.NoSuchRealmException;
 
 /**
- * This is a wrapper for Maven's main function that reads 2 property
- * files: debian/auto.properties and debian/manual.properties and adds
- * their content to maven's commandline.
+ * This is a wrapper for the main function of Maven 2.
  */
-public class Wrapper {
+public class Wrapper extends WrapperBase {
 
-    /** Holds extra properties that are read from property files */
-    private static Properties extraProperties = new Properties();
+    public static int main(String[] args, ClassWorld classWorld) throws IOException {
+        init(args);
 
-    /** The extended command line for maven's main function */
-    private static String[] newArgs;
+        int result = -1;
 
-    /** 
-     * Opens the filename specified by property 'key' and loads its
-     * properties into extraProperties
-     */
-    public static void updateProperties(String key) throws IOException {
-        String filename = System.getProperty(key);
-        if (filename == null) {
-            return;
-        }
-        FileInputStream stream = null;
         try {
-            stream = new FileInputStream(filename);
-            extraProperties.load(stream);
-        } finally {
-            if (stream != null) {
-                stream.close();
-            }
-        }
-    }
+            ClassRealm realm = classWorld.getRealm("debian");
 
-    /**
-     * Fill new commandline array 'newArgs' with properties from
-     * extraProperties and the current commandline array 'args.
-     */
-    public static void updateCommandLine(String[] args) throws IOException {
-        int argsSize = args.length;
-        int extraSize = extraProperties.size();
+            Class cwClass = realm.loadClass(ClassWorld.class.getName());
+            Class mainClass = realm.loadClass("org.apache.maven.cli.MavenCli");
+            Method mainMethod = mainClass.getMethod("main", new Class[]{ String[].class, cwClass });
 
-        newArgs = new String[argsSize + extraSize];
+            Object ret = mainMethod.invoke(mainClass, new Object[]{ newArgs, classWorld } );
 
-        int i = 0;
-        for (Enumeration e = extraProperties.propertyNames(); e.hasMoreElements(); ) {
-            String key = (String) e.nextElement();
-            String value = extraProperties.getProperty(key);
-            newArgs[i] = "-D" + key + "=" + value;
-            i++;
+            result = ((Integer)ret).intValue();
+        } catch (NoSuchRealmException e) {
+            System.err.println("ClassWorld realm 'debian' not found!");
+        } catch (Exception e) {
+            System.err.println("Unable to invoke Maven main method: " + e.toString());
+        } finally {
+            return result;
         }
-
-        System.arraycopy(args, 0, newArgs, extraSize, argsSize);
-    }
-
-    /**
-     * Wraps maven's main function
-     */
-    public static int main(String[] args, ClassWorld classWorld) throws IOException {
-        updateProperties("properties.file.manual");
-        
-        updateCommandLine(args);
-        
-        return MavenCli.main(newArgs, classWorld);
     }
 }
+
diff --git a/maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java b/maven-debian-helper/src/main/java/org/debian/maven/WrapperBase.java
similarity index 81%
copy from maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java
copy to maven-debian-helper/src/main/java/org/debian/maven/WrapperBase.java
index 3348850..8008ab0 100644
--- a/maven-debian-helper/src/main/java/org/debian/maven/Wrapper.java
+++ b/maven-debian-helper/src/main/java/org/debian/maven/WrapperBase.java
@@ -1,5 +1,8 @@
+package org.debian.maven;
+
 /*
  * Copyright 2009 Torsten Werner.
+ * Copyright 2013 Debian Java Developers.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,28 +17,23 @@
  * limitations under the License.
  */
 
-package org.debian.maven;
-
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.Enumeration;
 import java.util.Properties;
 
-import org.apache.maven.cli.MavenCli;
-import org.codehaus.classworlds.ClassWorld;
-
 /**
- * This is a wrapper for Maven's main function that reads 2 property
- * files: debian/auto.properties and debian/manual.properties and adds
- * their content to maven's commandline.
+ * This is a base class for Maven's main function wrappers. A subclass for
+ * specific Maven version should read 2 property files: debian/auto.properties
+ * and debian/manual.properties and add their content to maven's commandline.
  */
-public class Wrapper {
+public class WrapperBase {
 
     /** Holds extra properties that are read from property files */
     private static Properties extraProperties = new Properties();
 
     /** The extended command line for maven's main function */
-    private static String[] newArgs;
+    protected static String[] newArgs;
 
     /** 
      * Opens the filename specified by property 'key' and loads its
@@ -78,14 +76,9 @@ public class Wrapper {
         System.arraycopy(args, 0, newArgs, extraSize, argsSize);
     }
 
-    /**
-     * Wraps maven's main function
-     */
-    public static int main(String[] args, ClassWorld classWorld) throws IOException {
+    public static void init(String[] args) throws IOException {
         updateProperties("properties.file.manual");
-        
+
         updateCommandLine(args);
-        
-        return MavenCli.main(newArgs, classWorld);
     }
 }
diff --git a/maven-debian-helper/src/main/java/org/debian/maven/WrapperMaven3.java b/maven-debian-helper/src/main/java/org/debian/maven/WrapperMaven3.java
new file mode 100644
index 0000000..43f318c
--- /dev/null
+++ b/maven-debian-helper/src/main/java/org/debian/maven/WrapperMaven3.java
@@ -0,0 +1,54 @@
+package org.debian.maven;
+
+/*
+ * Copyright 2013 Debian Java Developers.
+ *
+ * Licensed 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.lang.reflect.Method;
+
+import org.codehaus.plexus.classworlds.ClassWorld;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
+
+/**
+ * This is a wrapper for the main function of Maven 3.
+ */
+public class WrapperMaven3 extends WrapperBase {
+
+    public static int main(String[] args, ClassWorld classWorld) throws IOException {
+        init(args);
+
+        int result = -1;
+
+        try {
+            ClassRealm realm = classWorld.getRealm("debian");
+
+            Class cwClass = realm.loadClass(ClassWorld.class.getName());
+            Class mainClass = realm.loadClass("org.apache.maven.cli.MavenCli");
+            Method mainMethod = mainClass.getMethod("main", new Class[]{ String[].class, cwClass });
+
+            Object ret = mainMethod.invoke(mainClass, new Object[]{ newArgs, classWorld } );
+
+            result = ((Integer)ret).intValue();
+        } catch (NoSuchRealmException e) {
+            System.err.println("ClassWorld realm 'debian' not found!");
+        } catch (Exception e) {
+            System.err.println("Unable to invoke Maven main method: " + e.toString());
+        } finally {
+            return result;
+        }
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/maven-debian-helper.git



More information about the pkg-java-commits mailing list