automate generation of stripped jsf-api.jar for publishing as official JSF API jar
http://java.net/jira/browse/JAVASERVERFACES-1977
SECTION: Modified Files
----------------------------
M jsf-tools/pom.xml
- Make it so tools.jar from the java platform is included in the build dependencies.
M common/ant/dependencies.xml
- Declare javaee-api.jar and necessary sugar.
M common/ant/common.xml
- Don't hard-code the reference to a version in the declaration for jsf-tools.jar
M jsf-api/build.xml
- Lots of ant nonsense to eventually invoke Peter von der Ahé's code.
A jsf-tools/src/main/java/com/sun/faces/tools/StripClassesForApiJar.java
- The heart of this commit was taken from an email forwarded to my by
Ryan Lubke some time in 2008. Hooray for email archiving. The code
was originally written by Peter von der Ahé, who can now be found at
Google and whose blog is at <
http://digital-sushi.org/about/>. The
rest of the commit is really just ant nonsense.
SECTION: Diffs
----------------------------
Index: jsf-tools/src/main/java/com/sun/faces/tools/StripClassesForApiJar.java
===================================================================
--- jsf-tools/src/main/java/com/sun/faces/tools/StripClassesForApiJar.java (revision 0)
+++ jsf-tools/src/main/java/com/sun/faces/tools/StripClassesForApiJar.java (revision 0)
@@ -0,0 +1,293 @@
+
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common Development
+ * and Distribution License("CDDL") (collectively, the "License"). You
+ * may not use this file except in compliance with the License. You can
+ * obtain a copy of the License at
+ *
https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
+ * or packager/legal/LICENSE.txt. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ * When distributing the software, include this License Header Notice in each
+ * file and include the License file at packager/legal/LICENSE.txt.
+ *
+ * GPL Classpath Exception:
+ * Oracle designates this particular file as subject to the "Classpath"
+ * exception as provided by Oracle in the GPL Version 2 section of the License
+ * file that accompanied this code.
+ *
+ * Modifications:
+ * If applicable, add the following below the License Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyright [year] [name of copyright owner]"
+ *
+ * Contributor(s):
+ * If you wish your version of this file to be governed by only the CDDL or
+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
+ * elects to include this software in this distribution under the [CDDL or GPL
+ * Version 2] license." If you don't indicate a single choice of license, a
+ * recipient has the option to distribute your version of this file under
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
+ * its licensees as provided above. However, if you add GPL Version 2 code
+ * and therefore, elected the GPL Version 2 license, then the option applies
+ * only if the new code is made subject to such option by the copyright
+ * holder.
+ */
+
+package com.sun.faces.tools;
+
+
+
+import com.sun.tools.javac.api.JavacTaskImpl;
+import com.sun.tools.javac.code.Kinds;
+import com.sun.tools.javac.code.Scope;
+import com.sun.tools.javac.code.Symbol.*;
+import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.code.Type;
+import com.sun.tools.javac.jvm.ClassReader;
+import com.sun.tools.javac.jvm.ClassWriter;
+import com.sun.tools.javac.jvm.Pool;
+import com.sun.tools.javac.processing.JavacProcessingEnvironment;
+import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.Name;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.Collections;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedOptions;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.Element;
+import javax.tools.Diagnostic;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileManager.Location;
+import javax.tools.JavaFileObject;
+import static javax.tools.JavaFileObject.Kind.CLASS;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.StandardLocation;
+import javax.tools.ToolProvider;
+
+/**
+ * Used to generate a "symbol file" representing rt.jar that only
+ * includes supported or legacy proprietary API. Valid annotation
+ * processor options:
+ *
+ * <dl>
+ * <dt>com.sun.tools.javac.sym.Jar</dt>
+ * <dd>Specifies the location of rt.jar.</dd>
+ * <dt>com.sun.tools.javac.sym.Dest</dt>
+ * <dd>Specifies the destination directory.</dd>
+ * </dl>
+ *
+ * <p><b>This is NOT part of any API supported by Sun Microsystems.
+ * If you write code that depends on this, you do so at your own
+ * risk. This code and its internal interfaces are subject to change
+ * or deletion without notice.</b></p>
+ *
+ * @author Peter von der Ah\u00e9
+ */
+_at_SupportedOptions({"com.sun.tools.javac.sym.Jar","com.sun.tools.javac.sym.ExtraApiClassPath","com.sun.tools.javac.sym.Dest"})
+_at_SupportedAnnotationTypes("*")
+public class StripClassesForApiJar extends AbstractProcessor {
+
+ static Set<String> getLegacyPackages() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
+ if (!renv.processingOver())
+ return true;
+
+ Set<String> legacy = getLegacyPackages();
+ Set<String> legacyProprietary = getLegacyPackages();
+ Set<String> documented = new HashSet<String>();
+ Set<PackageSymbol> packages =
+ ((JavacProcessingEnvironment)processingEnv).getSpecifiedPackages();
+ String jarName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Jar");
+ if (jarName == null)
+ throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Jar=LOCATION_OF_JAR");
+ String extraApiClassPath = processingEnv.getOptions().get("com.sun.tools.javac.sym.ExtraApiClassPath");
+ if (extraApiClassPath == null)
+ throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.ExtraApiClassPath=extra api class path");
+ String destName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Dest");
+ if (destName == null)
+ throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Dest=LOCATION_OF_JAR");
+
+ for (PackageSymbol psym : packages) {
+ String name = psym.getQualifiedName().toString();
+ legacyProprietary.remove(name);
+ documented.add(name);
+ }
+
+ JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+ StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
+ Location jarLocation = StandardLocation.locationFor(jarName);
+ File jarFile = new File(jarName);
+ String [] segments = extraApiClassPath.split(File.pathSeparator);
+ java.util.List<File> extraClassPathSegments = new ArrayList<File>(segments.length);
+ for (String cur : segments) {
+ extraClassPathSegments.add(new File(cur));
+ }
+ File ExtraApiClassPathFile = new File(extraApiClassPath);
+ try {
+ fm.setLocation(jarLocation, List.of(jarFile));
+ fm.setLocation(StandardLocation.CLASS_PATH, List.<File>nil());
+ fm.setLocation(StandardLocation.SOURCE_PATH, List.<File>nil());
+ {
+ ArrayList<File> bootClassPath = new ArrayList<File>();
+ bootClassPath.add(jarFile);
+ for (File cur : extraClassPathSegments) {
+ bootClassPath.add(cur);
+ }
+ // ADD EXTRA DEPENDENCIES HERE:
+ for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) {
+ // if (!new File(path.getName()).equals(new File("rt.jar")))
+ bootClassPath.add(path);
+ }
+ System.err.println("Using boot class path = " + bootClassPath);
+ fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
+ }
+ // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH));
+ File destDir = new File(destName);
+ if (!destDir.exists())
+ if (!destDir.mkdirs())
+ throw new RuntimeException("Could not create " + destDir);
+ fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir));
+ } catch (IOException ioe) {
+ System.err.println("Unable to set location");
+ return false;
+ }
+ Set<String> hiddenPackages = new HashSet<String>();
+ Set<String> crisp = new HashSet<String>();
+ List<String> options = List.of("-XDdev");
+ // options = options.prepend("-doe");
+ // options = options.prepend("-verbose");
+ JavacTaskImpl task = (JavacTaskImpl)
+ tool.getTask(null, fm, null, options, null, null);
+ com.sun.tools.javac.main.JavaCompiler compiler = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
+ ClassReader reader = ClassReader.instance(task.getContext());
+ ClassWriter writer = ClassWriter.instance(task.getContext());
+ Name.Table names = Name.Table.instance(task.getContext());
+ Type.moreInfo = true;
+ Pool pool = new Pool();
+ ClassSymbol cs = null;
+ try {
+ for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) {
+ String className = fm.inferBinaryName(jarLocation, file);
+ int index = className.lastIndexOf('.');
+ String pckName = index == -1 ? "" : className.substring(0, index);
+ if (documented.contains(pckName)) {
+ if (!legacy.contains(pckName))
+ crisp.add(pckName);
+ // System.out.println("Documented: " + className);
+ } else if (legacyProprietary.contains(pckName)) {
+ // System.out.println("Legacy proprietary: " + className);
+ } else {
+ // System.out.println("Hidden " + className);
+ hiddenPackages.add(pckName);
+ continue;
+ }
+ // PackageSymbol psym = reader.enterPackage(names.fromString(pckName));
+ // psym.complete();
+ TypeSymbol sym = (TypeSymbol)compiler.resolveIdent(className);
+ if (sym.kind != Kinds.TYP) {
+ if (className.indexOf('$') < 0) {
+ System.err.println("Ignoring (other) " + className + " : " + sym);
+ System.err.println(" " + sym.getClass().getSimpleName() + " " + sym.type);
+ }
+ continue;
+ }
+ sym.complete();
+ if (sym.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
+ System.err.println("Ignoring (bad) " + sym.getQualifiedName());
+ }
+ if (false) {
+ /*
+ * Following eliminates non-public classes from output,
+ * but is too aggressive because it also eliminates
+ * non-public superclasses of public classes, which
+ * makes the output unusable.
+ */
+ if (sym.owner.kind == Kinds.PCK &&
+ (sym.flags() & Flags.AccessFlags) == Flags.PUBLIC) {
+ cs = (ClassSymbol) sym;
+ writeClass(pool, cs, writer);
+ cs = null;
+ }
+ } else {
+ cs = (ClassSymbol) sym;
+ writeClass(pool, cs, writer);
+ cs = null;
+ }
+ }
+ } catch (IOException ex) {
+ reportError(ex, cs);
+ } catch (CompletionFailure ex) {
+ reportError(ex, cs);
+ } catch (RuntimeException ex) {
+ reportError(ex, cs);
+ }
+ if (false) {
+ for (String pckName : crisp)
+ System.out.println("Crisp: " + pckName);
+ for (String pckName : hiddenPackages)
+ System.out.println("Hidden: " + pckName);
+ for (String pckName : legacyProprietary)
+ System.out.println("Legacy proprietary: " + pckName);
+ for (String pckName : documented)
+ System.out.println("Documented: " + pckName);
+ }
+
+ return true;
+ }
+
+ void reportError(Throwable ex, Element element) {
+ if (element != null)
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ ex.getLocalizedMessage(),
+ element);
+ else
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ ex.getLocalizedMessage());
+ }
+
+ void writeClass(final Pool pool, final ClassSymbol cs, final ClassWriter writer) {
+ try {
+ pool.reset();
+ cs.pool = pool;
+ writer.writeClass(cs);
+ for (Scope.Entry e = cs.members().elems; e != null; e = e.sibling) {
+ if (e.sym.kind == Kinds.TYP) {
+ ClassSymbol nestedClass = (ClassSymbol)e.sym;
+ nestedClass.complete();
+ writeClass(pool, nestedClass, writer);
+ }
+ }
+ } catch (ClassWriter.StringOverflow ex) {
+ throw new RuntimeException(ex);
+ } catch (ClassWriter.PoolOverflow ex) {
+ throw new RuntimeException(ex);
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ public SourceVersion getSupportedSourceVersion() {
+ return SourceVersion.latest();
+ }
+
+}
Index: jsf-tools/pom.xml
===================================================================
--- jsf-tools/pom.xml (revision 8905)
+++ jsf-tools/pom.xml (working copy)
@@ -94,4 +94,26 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
+
+ <profiles>
+ <profile>
+ <id>default-tools.jar</id>
+ <activation>
+ <property>
+ <name>java.vendor</name>
+ <value>Sun Microsystems Inc.</value>
+ </property>
+ </activation>
+ <dependencies>
+ <dependency>
+ <groupId>com.sun</groupId>
+ <artifactId>tools</artifactId>
+ <version>1.6.0</version>
+ <scope>system</scope>
+ <systemPath>${java.home}/../lib/tools.jar</systemPath>
+ </dependency>
+ </dependencies>
+ </profile>
+ </profiles>
+
</project>
Index: common/ant/dependencies.xml
===================================================================
--- common/ant/dependencies.xml (revision 8906)
+++ common/ant/dependencies.xml (working copy)
@@ -66,6 +66,7 @@
<property name="taglibdoc.version" value="1.3"/>
<property name="jsdoc.version" value="2.0.2"/>
<property name="servlet.api.version" value="2.5"/>
+ <property name="javaee-api.version" value="6.0"/>
<property name="newer.servlet.api.version" value="3.0"/>
<property name="jsp.api.version" value="2.1"/>
<property name="jstl.api.version" value="1.2"/>
@@ -124,6 +125,7 @@
value="${dependency.jar.dir}/maven-repository-importer-${maven.repository.importer.version}.jar"/>
<property name="servlet.jar" value="${dependency.jar.dir}/servlet-api-${servlet.api.version}.jar"/>
<property name="servlet3.jar" value="${dependency.jar.dir}/servlet-api-${newer.servlet.api.version}.jar"/>
+ <property name="javaee-api.jar" value="${dependency.jar.dir}/javaee-api-${javaee-api.version}.jar"/>
<property name="jsp.jar" value="${dependency.jar.dir}/jsp-api-${jsp.api.version}.jar"/>
<property name="el-api.jar" value="${dependency.jar.dir}/el-api-${el.version}.jar"/>
<property name="el-impl.jar" value="${dependency.jar.dir}/el-impl-${el.version}.jar"/>
@@ -319,6 +321,18 @@
</then>
</if>
+ <!-- JAVAEE API -->
+ <if>
+ <not>
+ <available file="${javaee-api.jar}" property="ignored"/>
+ </not>
+ <then>
+ <get src="${java.net.maven2}/javax/javaee-api/${javaee-api.version}/javaee-api-${javaee-api.version}.jar"
+ dest="${javaee-api.jar}"
+ usetimestamp="true"/>
+
+ </then>
+ </if>
<!-- SERVLET API -->
<if>
<not>
Index: common/ant/common.xml
===================================================================
--- common/ant/common.xml (revision 8905)
+++ common/ant/common.xml (working copy)
@@ -204,7 +204,7 @@
<!-- this is hard coded to match the values in ${basedir}/jsf-tools/pom.xml -->
<property name="jsf-tools.jar"
- value="${maven.repo.local}/com/sun/faces/build/jsf-tools/2.1.0-SNAPSHOT/jsf-tools-2.1.0-SNAPSHOT.jar" />
+ value="${maven.repo.local}/com/sun/faces/build/jsf-tools/${snapshot.version}/jsf-tools-${snapshot.version}.jar" />
<!-- Test classpaths that are used in multiple places within the project -->
<path id="junit.classpath">
Index: jsf-api/build.xml
===================================================================
--- jsf-api/build.xml (revision 8905)
+++ jsf-api/build.xml (working copy)
@@ -833,7 +833,82 @@
<mvn.deploy.release.local type="api"/>
</target>
- <target name="mvn.deploy.api.release.local">
+ <target name="strip.api.jar"
+ depends="get.faces.java.packages.as.space.separated.string">
+ <!-- path maintenance -->
+ <move file="${build.lib.dir}/${name}.jar" failonerror="true"
+ tofile="${build.lib.dir}/${name}-full.jar" />
+ <delete failonerror="false" dir="${build.lib.dir}/jsf-api-stripped" />
+ <mkdir dir="${build.lib.dir}/jsf-api-stripped" />
+ <!-- windows or unix java exec invocation -->
+ <condition property="exec.name" value="cmd.exe">
+ <os family="windows"/>
+ </condition>
+ <condition property="exec.arg.1"
+ value="/c ${java.home}/bin/javac">
+ <os family="windows"/>
+ </condition>
+ <property name="exec.name" value="${java.home}/bin/javac"/>
+ <property name="exec.arg.1" value=""/>
+ <!-- invoke the javac processor to produce stripped .class files -->
+ <exec executable="${exec.name}" failonerror="true">
+ <arg line="${exec.arg.1}"/>
+ <arg line="-XDprocess.packages"/>
+ <arg line="-proc:only"/>
+ <arg line="-cp ${build.lib.dir}/${name}-full.jar:${jsf-tools.jar}"/>
+ <arg line="-processor com.sun.faces.tools.StripClassesForApiJar"/>
+ <arg line="-Acom.sun.tools.javac.sym.Jar=${build.lib.dir}/${name}-full.jar"/>
+ <arg line="-Acom.sun.tools.javac.sym.ExtraApiClassPath=${javaee-api.jar}"/>
+ <arg line="-Acom.sun.tools.javac.sym.Dest=${build.lib.dir}/jsf-api-stripped"/>
+ <arg line="${src.paths}"/>
+ </exec>
+ <!-- jar up the stripped files into jsf-api.jar. -->
+ <!-- Extract the original manifest -->
+ <unjar src="${build.lib.dir}/${name}-full.jar"
+ dest="${build.lib.dir}/jsf-api-stripped">
+ <patternset>
+ <include name="META-INF/MANIFEST.MF" />
+ </patternset>
+ </unjar>
+ <jar destfile="${build.lib.dir}/${name}.jar"
+ basedir="${build.lib.dir}/jsf-api-stripped"
+ manifest="${build.lib.dir}/jsf-api-stripped/META-INF/MANIFEST.MF" />
+ </target>
+
+ <target name="get.faces.java.packages.as.space.separated.string"
+ description="Traverse ${src.dir} and extract the directory names into a property ${src.paths} as a space separated list of java packages">
+ <!-- Suck the dirs into a path-like structure. -->
+ <path id="src.dir.path">
+ <dirset dir="${src.dir}" />
+ </path>
+ <!-- Suck the path-like structure into a property. -->
+ <property name="src.dir.paths" refid="src.dir.path" />
+
+ <!-- Massage the property value to make it be as desired. -->
+
+ <!-- Remove the leading fully qualified paths. -->
+ <propertyregex property="src.paths" input="${src.dir.paths}" override="true"
+ regexp="${src.dir}" replace="" global="true" />
+ <!-- Remove the leading "/" from every path entry. -->
+ <propertyregex property="src.paths" input="${src.paths}" override="true"
+ regexp="/javax"
+ replace="javax" global="true" />
+ <!-- Remove the javax entry. It is not a java package. -->
+ <propertyregex property="src.paths" input="${src.paths}" override="true"
+ regexp="${path.separator}javax${path.separator}"
+ replace="" global="true" />
+ <!-- Replace ':' with ' ' (or ';' with ' ' on windows). -->
+ <propertyregex property="src.paths" input="${src.paths}" override="true"
+ regexp="${path.separator}"
+ replace=" " global="true" />
+ <!-- Replace '/' with '.'. -->
+ <propertyregex property="src.paths" input="${src.paths}" override="true"
+ regexp="/"
+ replace="." global="true" />
+ </target>
+
+ <target name="mvn.deploy.api.release.local" depends="strip.api.jar">
+
<mvn.deploy.release.local groupId="javax.faces" version="${spec.version}" type="api"/>
</target>
@@ -856,7 +931,7 @@
<mvn.deploy.release type="api"/>
</target>
- <target name="mvn.deploy.api.release">
+ <target name="mvn.deploy.api.release" depends="strip.api.jar">
<mvn.deploy.release groupId="javax.faces" version="${spec.version}" type="api"/>
</target>
--
| edward.burns_at_oracle.com | office: +1 407 458 0017
| homepage: | http://ridingthecrest.com/