You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by gi...@apache.org on 2018/03/21 15:36:16 UTC

ant-ivy git commit: IVY-1321 Make Main.run() public

Repository: ant-ivy
Updated Branches:
  refs/heads/master 39edf7ea3 -> 6c6b44161


IVY-1321 Make Main.run() public

Project: http://git-wip-us.apache.org/repos/asf/ant-ivy/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant-ivy/commit/6c6b4416
Tree: http://git-wip-us.apache.org/repos/asf/ant-ivy/tree/6c6b4416
Diff: http://git-wip-us.apache.org/repos/asf/ant-ivy/diff/6c6b4416

Branch: refs/heads/master
Commit: 6c6b4416125c62e35c3e2a279f20180b7b3d23b9
Parents: 39edf7e
Author: Gintas Grigelionis <gi...@apache.org>
Authored: Wed Mar 21 16:36:01 2018 +0100
Committer: Gintas Grigelionis <gi...@apache.org>
Committed: Wed Mar 21 16:36:01 2018 +0100

----------------------------------------------------------------------
 asciidoc/release-notes.adoc       |  2 ++
 src/java/org/apache/ivy/Main.java | 66 +++++++++++++++++++++++++++++-----
 2 files changed, 60 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/6c6b4416/asciidoc/release-notes.adoc
----------------------------------------------------------------------
diff --git a/asciidoc/release-notes.adoc b/asciidoc/release-notes.adoc
index fdfd491..d24f40b 100644
--- a/asciidoc/release-notes.adoc
+++ b/asciidoc/release-notes.adoc
@@ -79,6 +79,7 @@ For details about the following changes, check our JIRA install at link:https://
 - FIX: Allow conditionally activated properties through Maven profiles, to be referenced in the `pom.xml` (jira:IVY-1577[])
 - FIX: Fix Maven dependency resolution for dependencies with multiple different classifiers (jira:IVY-1576[])
 - FIX: Correct parent classloader when lauching resolved artifacts from CLI (jira:IVY-1483[]) (Thanks to Stephan Feder)
+- FIX: Make Main.run() public (usable with REPL) (jira:IVY-1321[] jira:IVY-1578[]) (Thanks to Greg Perry)
 
 - IMPROVEMENT: Throw an IllegalStateException when retrieving the resolutionCacheRoot on the DefaultResolutionCacheManager if the basedir (or IvySettings) is not set (jira:IVY-1482[])
 - IMPROVEMENT: Optimization: limit the revision numbers scanned if revision prefix is specified (Thanks to Ernestas Vaiciukevi&ccaron;ius)
@@ -211,6 +212,7 @@ Here is the list of people who have contributed source code and documentation up
 * Douglas Palmer
 * Jesper Pedersen
 * Emmanuel Pellereau
+* Greg Perry
 * Carsten Pfeiffer
 * Yanus Poluektovich
 * Roshan Punnoose

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/6c6b4416/src/java/org/apache/ivy/Main.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/Main.java b/src/java/org/apache/ivy/Main.java
index 516448e..e959bd5 100644
--- a/src/java/org/apache/ivy/Main.java
+++ b/src/java/org/apache/ivy/Main.java
@@ -44,6 +44,7 @@ import org.apache.ivy.core.publish.PublishOptions;
 import org.apache.ivy.core.report.ArtifactDownloadReport;
 import org.apache.ivy.core.report.ResolveReport;
 import org.apache.ivy.core.resolve.ResolveOptions;
+import org.apache.ivy.core.resolve.ResolveProcessException;
 import org.apache.ivy.core.retrieve.RetrieveOptions;
 import org.apache.ivy.core.settings.IvySettings;
 import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter;
@@ -219,31 +220,67 @@ public final class Main {
     }
 
     public static void main(String[] args) throws Exception {
-        CommandLineParser parser = getParser();
         try {
-            run(parser, args);
+            run(args, true);
             System.exit(0);
         } catch (ParseException ex) {
             System.err.println(ex.getMessage());
-            usage(parser, false);
             System.exit(1);
         }
     }
 
-    @SuppressWarnings("deprecation")
+    /**
+     * A method that allows to run embedded Ivy as if it were launched from CLI
+     * (e.g., for use with REPL in Java 9+).
+     *
+     * @param args an array of command line arguments
+     * @return null if asked for usage or version, or if anything fails during resolve, publish or
+     * launch; a ResolveReport on success
+     * @throws Exception if something goes wrong
+     */
+    public static ResolveReport run(String[] args) throws Exception {
+        return run(args, false);
+    }
+
+    /*
+     * For backwards compatibility and testing
+     */
     static void run(CommandLineParser parser, String[] args) throws Exception {
+        if (Arrays.asList(args).contains("-?")) {
+            usage(parser, false);
+            return;
+        }
+
+        run(parser.parse(args), true);
+    }
+
+    private static ResolveReport run(String[] args, boolean isCli) throws Exception {
+        CommandLineParser parser = getParser();
+
         // parse the command line arguments
-        CommandLine line = parser.parse(args);
+        CommandLine line;
+        try {
+            line = parser.parse(args);
+        } catch (ParseException pe) {
+            // display usage and and rethrow
+            usage(parser, false);
+            throw new ParseException(pe.getMessage());
+        }
 
         if (line.hasOption("?")) {
             usage(parser, line.hasOption("deprecated"));
-            return;
+            return null;
         }
 
+        return run(line, isCli);
+    }
+
+    @SuppressWarnings("deprecation")
+    private static ResolveReport run(CommandLine line, boolean isCli) throws Exception {
         if (line.hasOption("version")) {
             System.out.println("Apache Ivy " + Ivy.getIvyVersion() + " - " + Ivy.getIvyDate()
                     + " :: " + Ivy.getIvyHomeURL());
-            return;
+            return null;
         }
 
         boolean validate = !line.hasOption("novalidate");
@@ -316,7 +353,18 @@ public final class Main {
         }
         ResolveReport report = ivy.resolve(ivyfile.toURI().toURL(), resolveOptions);
         if (report.hasError()) {
-            System.exit(1);
+            if (isCli) {
+                System.exit(1);
+            }
+
+            StringBuilder sb = new StringBuilder();
+            for (String problem : report.getAllProblemMessages()) {
+                if (sb.length() > 0) {
+                    sb.append("\n");
+                }
+                sb.append(problem);
+            }
+            throw new ResolveProcessException(sb.toString());
         }
         ModuleDescriptor md = report.getModuleDescriptor();
 
@@ -392,6 +440,8 @@ public final class Main {
         }
         ivy.getLoggerEngine().popLogger();
         ivy.popContext();
+
+        return report;
     }
 
     /**