Browse Source

Extract front end class info from Jar

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272187 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 23 years ago
parent
commit
fe7c38e3a0
2 changed files with 47 additions and 5 deletions
  1. +6
    -2
      proposal/mutant/build.xml
  2. +41
    -3
      proposal/mutant/src/java/start/org/apache/ant/start/Main.java

+ 6
- 2
proposal/mutant/build.xml View File

@@ -84,7 +84,11 @@
<javac destdir="${bin.dir}/cli" srcdir="${java.dir}/cli" debug="${debug}">
<classpath refid="classpath.cli"/>
</javac>
<jar basedir="${bin.dir}/cli" jarfile="${distlib.dir}/frontend/cli.jar"/>
<jar basedir="${bin.dir}/cli" jarfile="${distlib.dir}/frontend/cli.jar">
<manifest>
<attribute name="Main-Class" value="org.apache.ant.cli.Commandline"/>
</manifest>
</jar>
</target>

<target name="start" depends="init">
@@ -199,7 +203,7 @@
<mkdir dir="${javadocs.dir}"/>
<javadoc packagenames="org.apache.*"
useexternalfile="yes"
sourcepath="${java.dir}/antcore:${java.dir}/init:${java.dir}/common:${java.dir}/frontend:${java.dir}/start"
sourcepath="${java.dir}/antcore:${java.dir}/init:${java.dir}/common:${java.dir}/cli:${java.dir}/start"
destdir="${javadocs.dir}"
author="true"
private ="true"


+ 41
- 3
proposal/mutant/src/java/start/org/apache/ant/start/Main.java View File

@@ -53,9 +53,13 @@
*/
package org.apache.ant.start;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import org.apache.ant.init.InitConfig;

/**
@@ -67,9 +71,8 @@ import org.apache.ant.init.InitConfig;
*/
public class Main {
/** The actual class that implements the command line front end. */
public static final String COMMANDLINE_CLASS
public static final String DEFAULT_COMMANDLINE_CLASS
= "org.apache.ant.cli.Commandline";

/** The default front end name */
public static final String DEFAULT_FRONTEND = "cli";
@@ -103,8 +106,14 @@ public class Main {
//System.out.println("Front End Loader config");
//LoaderUtils.dumpLoader(System.out, frontEndLoader);

String mainClass = getMainClass(frontendJar);
System.out.println("Front end main class = " + mainClass);
if (mainClass == null) {
mainClass = DEFAULT_COMMANDLINE_CLASS;
}
// Now start the front end by reflection.
Class commandLineClass = Class.forName(COMMANDLINE_CLASS, true,
Class commandLineClass = Class.forName(mainClass, true,
frontEndLoader);

final Class[] param = {Class.forName("[Ljava.lang.String;"),
@@ -117,5 +126,34 @@ public class Main {
e.printStackTrace();
}
}
/**
* Pick up the main class from a jar's manifest
*
* @param jarURL the URL to the jar
* @return the jar's main-class or null if it is not specified or cannot be
* determined.
*/
private String getMainClass(URL jarURL) {
try {
JarInputStream stream = null;
try {
stream = new JarInputStream(jarURL.openStream());
Manifest manifest = stream.getManifest();
if (manifest == null) {
return null;
}
Attributes mainAttributes = manifest.getMainAttributes();
return mainAttributes.getValue("Main-Class");
} finally {
if (stream != null) {
stream.close();
}
}
} catch (IOException e) {
// ignore
return null;
}
}
}


Loading…
Cancel
Save