[Jython-checkins] jython: Make the Gradle-built JAR a published artefact.

jeff.allen jython-checkins at python.org
Sat Sep 21 06:58:50 EDT 2019


https://hg.python.org/jython/rev/cdf832cb2e82
changeset:   8291:cdf832cb2e82
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Tue Sep 17 22:43:11 2019 +0100
summary:
  Make the Gradle-built JAR a published artefact.

Introduces a ~/publications directory and places the bundles there,
adding "jython-slim" built by Gradle. Names in maven/build.xml are made
more consistent with build.xml. There are fixes to the Gradle build and
additions to manipulate the version comparably with the Ant build.

files:
  .hgignore       |    2 +
  build.gradle    |  144 +++++++++++++++++------------------
  build.xml       |    7 +-
  maven/build.xml |  109 +++++++++++++-------------
  settings.gradle |    2 +-
  5 files changed, 132 insertions(+), 132 deletions(-)


diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -30,11 +30,13 @@
 __pycache__
 bin
 
+# Jython specific
 ant.properties
 build
 build2
 cachedir
 dist
+publications
 reports
 
 profile.txt
diff --git a/build.gradle b/build.gradle
--- a/build.gradle
+++ b/build.gradle
@@ -36,13 +36,19 @@
  * application. It should be all you have to edit to version a release built
  * here. But of course you have to do it the hard way too (see build.xml) as
  * long as Ant is also used.
+ *
+ * The convention here is that you specify the version of the next release. If
+ * there are files not checked in, extra files (not of types hg ignores), or the
+ * changeset is not tagged correspondingly, the build will add "-SNAPSHOT" to
+ * the effective version.
  */
 // Versions are specified in this grammar:
-// <major> . <minor> ( . <micro> )? ( <release> <serial> )? ([-+] <word>? )?
+// <major> . <minor> ( . <micro> )? ( <release> <serial> )? ( + | - <word> )?
 
 version = '2.7.2a1+'
 
 // Valid examples (please preserve in comments):
+//version = '2.7.2a1+'
 //version = '2.7.2a2'
 //version = '2.7.2b1-SNAPSHOT'
 //version = '2.7.2b1'
@@ -306,9 +312,7 @@
 
     // Create the properties when the task runs. But do it before the write!
     doFirst {
-        /*
-         * Query Mercurial for version and tagging.
-         */
+        // Query Mercurial for version and tagging.
         String hgOutput = 'hg identify -ibt'.execute().text
         hgOutput = hgOutput.split('\n', 2)[0]
 
@@ -328,8 +332,8 @@
          * properties. (The Ant build.xml requires them to be set in parts, but
          * we can work it out from project.version.)
          */
-        // <major>.<minor>(.<micro>)(<release><serial>)?([-+]<snapshot>)?
-        def versionRegex = /(\d+)\.(\d+)(\.(\d+))?((a|b|rc)(\d+))?([-+]\w*)?/
+        // <major>.<minor>(.<micro>)(<release><serial>)?(+|-<snapshot>)?
+        def versionRegex = /(\d+)\.(\d+)(\.(\d+))?((a|b|rc)(\d+))?(\+|-(\w+))?/
         def versionMatcher = project.version =~ versionRegex
         if (versionMatcher.count != 1) {
             throw new IllegalArgumentException(
@@ -344,7 +348,6 @@
         String micro = versionResult[3] ? versionResult[4] : '0'
 
         // Convert the optional <release><serial> to numbers
-        int SNAPSHOT = 0xaa
         int level = 0, serial = 0
         if (versionResult[5]) {
             // This is some kind of pre-final release (unless snapshot)
@@ -359,12 +362,49 @@
             level = 0xf
         }
 
-        // Except, if there was something after the version we decoded ...
-        if (versionResult[8]) {
-            level = SNAPSHOT // it's snapshot or dev version of some kind
-            serial = 0
+        // Convert optional +|-<word> to -DEV or -SNAPSHOT suffix or empty string
+        String snapshotSuffix = versionResult[8];
+        if (snapshotSuffix == '+') {
+            snapshotSuffix = "-SNAPSHOT"
         }
 
+        /*
+         * Work out if it looks like a release, or adjust project.version. This logic prevents us
+         * releasing from a polluted repo (similar to logic in the Ant build.xml).
+         */
+        def L = [0:'', 10:'a', 11:'b', 12:'rc', 15:'']
+        String release = "$major.$minor.$micro${L[level]}${serial?:''}"
+
+        if (snapshotSuffix == null) {
+            // The version is named correctly for a release. Make safety checks on the repo.
+            String expectTag = "v$release"
+            String message = null;
+            if (ident.endsWith('+')) {
+                message = 'Version-controlled files have been edited since the last commit'
+            } else if (tag != expectTag) {
+                message = "Change set $ident is not tagged $expectTag."
+            } else {
+                // Query Mercurial for status: non-empty if uncontrolled (unignored) files.
+                String hgStatus = 'hg status'.execute().text
+                if (hgStatus.trim().length() > 0) {
+                    message = 'Workspace contains uncontrolled files'
+                }
+            }
+            // If a message was set for any reason, fall back to a snapshot.
+            if (message == null) {
+                // Repository state is good for a full build.
+                snapshotSuffix = ''
+            } else {
+                // Some reason found not to build the release.
+                println "$message - build is a snapshot."
+                snapshotSuffix = '-SNAPSHOT'
+            }
+        }
+
+        // Rebuild the version with the snapshot suffix, even if not given originally.
+        project.version = release + snapshotSuffix
+        println "This build is for v${project.version}."
+
         property('jython.version', project.version)
         property('jython.major_version', major)
         property('jython.minor_version', minor)
@@ -382,22 +422,6 @@
         property('jython.build.date',
                 (new SimpleDateFormat('MMM d yyyy'))
                 .format(project.ext.buildDate))
-
-        /*
-         * Remind the developer about tagging it if it looks like a release,
-         * or to adjust project.version if we're moving on from the release.
-         */
-        if (level != SNAPSHOT) {
-            def L = [0:'', 10:'a', 11:'b', 12:'rc', 15:'']
-            String release = "$major.$minor.$micro${L[level]}${serial?:''}"
-            println "This build is versioned for distribution as $release"
-            if (tag == 'tip' || ident.endsWith('+')) {
-                println "If this really is distributable, " +
-                        "don't forget to tag it in the repository.\n" +
-                        "Alternatively, add a suffix to version = " +
-                        "'${project.version}' in build.gradle to shut this up."
-            }
-        }
     }
 }
 
@@ -678,16 +702,15 @@
  * distribute.
  *
  * Maybe have a second JAR that contains the additional material necessary to
- * run integration tests (regression tests and others). Python tests are
- * (incorrectly) in the main JAR at present.
+ * run integration tests (regression tests and others).
  */
 
-task sourcesJar(type: Jar) {
+task sourcesJar(type: Jar, dependsOn: classes) {
     classifier = 'sources'
     from sourceSets.main.allJava
 }
 
-task javadocJar(type: Jar) {
+task javadocJar(type: Jar, dependsOn: javadoc) {
     classifier = 'javadoc'
     from javadoc.destinationDir
 }
@@ -708,14 +731,24 @@
     }
 
     repositories {
-        // Dummy one for test: need sonatype here and credentials from user
+        // Staging area where ant -f maven/build.xml will look.
         maven {
-            name = 'myRepo'
-            url = "file://${buildDir}/repo"
+            name = 'stagingRepo'
+            url = "file://${buildDir}/stagingRepo"
         }
     }
 }
 
+// Ensure version computation/branding precedes any publication we use.
+publish.dependsOn(generateVersionInfo)
+
+/* FIXME: Depending on publishMainPublicationToMavenLocal does not work,
+   because it does not exist when evaluating this line. Is this the deferred
+   configuration removed in Gradle 5.0? Failsd on POM version mismatch if main
+   publish task not run before publishMainPublicationToMavenLocal.
+*/
+//publishMainPublicationToMavenLocal.dependsOn(generateVersionInfo)
+
 
 // ---------------- Java unit tests --------------------------------------------
 
@@ -755,6 +788,8 @@
     systemProperty 'python.test.source.dir', project.ext.testSourceDir
     // Place cache outside the targets for jar task
     systemProperty 'python.cachedir', "${project.buildDir}/cachedir"
+    // Logging level: default is message=INFO
+    //systemProperty 'python.verbose', 'CONFIG'
 
     include '**/*Test*'
 
@@ -784,47 +819,6 @@
         println "systemProperties = $systemProperties"
     }
 
-    /* A reminder of how this is addressed in build.xml, for comparison:
-
-    <target name="javatest-basepath" depends="developer-build">
-        <mkdir dir="${junit.reports}"/>
-        <junit fork="true" printsummary="true">
-            <formatter type="xml"/>
-            <sysproperty key="python.home" value="${dist.dir}"/>
-            <sysproperty key="python.test.source.dir" value="${test.source.dir}"/>
-            <classpath refid="test.classpath"/>
-            <batchtest todir="${junit.reports}" skipNonTests="true">
-                <fileset dir="${test.source.dir}" includes="** /*Test*.java">
-                    <exclude name="** /InterpTestCase.java" />
-                    <exclude name="** /jythonTest*" /> <!-- Must run interactively -->
-                    <exclude name="org/python/antlr/**" />
-                    <exclude name="org/python/tests/imp/**" /> <!-- See importest -->
-                    <exclude name=".classpath" />
-                    <exclude name=".project" />
-                </fileset>
-            </batchtest>
-        </junit>
-    </target>
-
-    <target name="importest" depends="developer-build" description="run all the JUnit tests that need tests/python in the path.">
-        <mkdir dir="${junit.reports}"/>
-        <junit fork="true" printsummary="true">
-            <formatter type="xml"/>
-            <sysproperty key="python.home" value="${dist.dir}"/>
-            <sysproperty key="python.test.source.dir" value="${test.source.dir}"/>
-            <classpath refid="test.classpath"/>
-            <classpath refid="test.classpath"/>
-            <classpath>
-                <pathelement location="${jython.base.dir}/tests/python"/>
-            </classpath>
-            <batchtest todir="${junit.reports}">
-                <fileset dir="${test.source.dir}" includes="org/python/tests/imp/*Test*.java">
-                </fileset>
-            </batchtest>
-        </junit>
-    </target>
-
-    */
 }
 
 
diff --git a/build.xml b/build.xml
--- a/build.xml
+++ b/build.xml
@@ -168,6 +168,9 @@
         <property name="reports.dir" value="${basedir}/reports" />
         <property name="junit.dir" value="${reports.dir}/junit" />
         <property name="junit.reports" value="${junit.dir}/raw" />
+
+        <!-- Artefacts for publication. -->
+        <property name="pubs.dir" value="${basedir}/publications" />
     </target>
 
     <target name="common-jars" depends="common-dirs">
@@ -261,8 +264,6 @@
         <path id="test.classpath">
             <path refid="main.classpath"/>
             <filelist dir="${extlibs.dir}">
-                <file name="asm-commons-7.0.jar" />
-                <file name="asm-util-7.0.jar" />
                 <file name="junit-4.10.jar" />
             </filelist>
             <pathelement location="${exposed.dir}" />
@@ -407,6 +408,8 @@
             <fileset dir="${build.dir}" includes="**/*" defaultexcludes="false" />
             <!-- all files and subdirectories of ${dist.dir}, without ${dist.dir} itself. -->
             <fileset dir="${dist.dir}" includes="**/*" defaultexcludes="false" />
+            <!-- all files and subdirectories of ${pubs.dir}, without ${pubs.dir} itself. -->
+            <fileset dir="${pubs.dir}" includes="**/*" />
         </delete>
     </target>
 
diff --git a/maven/build.xml b/maven/build.xml
--- a/maven/build.xml
+++ b/maven/build.xml
@@ -32,21 +32,28 @@
   <property file="${user.home}/ant.properties" />
   <property file="${basedir}/default.properties"/>
 
+  <!-- Artefacts for publication. -->
+  <property name="pubs.dir" value="${basedir}/publications" />
+
   <property name="m2.repo" value="${user.home}/.m2/repository"/>
   <property name="m2.groupDir" value="org/python"/>
 
+  <property name="gradle.base" value="${basedir}/build2"/>
+  <property name="gradle.repo" value="${gradle.base}/stagingRepo"/>
+
   <property name="build.base" value="${basedir}/build"/>
-  <property name="build.dir" value="${build.base}/maven"/>
+  <property name="build.maven" value="${build.base}/maven"/>
   <property name="version.properties" value="${build.base}/classes/org/python/version.properties"/>
-  <property name="dist.base" value="${basedir}/dist"/>
+  <property name="dist.dir" value="${basedir}/dist"/>
   <property name="src.dir" value="${basedir}/src"/>
+  <property name="bundle.dir" value="${basedir}/bundle"/>
 
   <condition property="do.build">
     <not>
       <and>
         <available file="${version.properties}"/>
-        <available file="${dist.base}/jython.jar"/>
-        <available file="${dist.base}/jython-standalone.jar"/>
+        <available file="${dist.dir}/jython.jar"/>
+        <available file="${dist.dir}/jython-standalone.jar"/>
       </and>
     </not>
   </condition>
@@ -75,22 +82,28 @@
     <m2-bundle artifactId="jython-installer" version="${jython.version}"/>
   </target>
 
+  <target name="bundle-slim" depends="prepare" if="gradle.published">
+    <jar jarfile="${pubs.dir}/jython-slim-${jython.version}-bundle.jar">
+        <fileset dir="${gradle.repo}/${m2.groupDir}/jython-slim/${jython.version}">
+            <include name="*.pom"/>
+            <include name="*.jar"/>
+        </fileset>
+    </jar>
+  </target>
+
   <target name="install-all" depends="install, install-standalone"/>
 
-  <target name="bundle-all" depends="version, bundle, bundle-standalone, bundle-installer"/>
+  <target name="bundle-all"
+          depends="version, bundle, bundle-standalone, bundle-installer, bundle-slim"/>
 
   <target name="clean">
-    <delete dir="${build.dir}"/>
-
+    <delete dir="${build.maven}"/>
     <delete>
-      <fileset dir="${dist.base}" includes="*-bundle.jar"/>
+      <fileset dir="${bundle.dir}" includes="*-bundle.jar"/>
     </delete>
   </target>
 
-  <!--
-       Installs artifact to the local repo specified.
-  -->
-
+  <!-- Install an artifact to the local repo specified by ${m2.repo}. -->
   <macrodef name="m2-install">
     <attribute name="artifactId"/>
     <attribute name="version"/>
@@ -110,9 +123,9 @@
         </filterset>
       </copy>
 
-      <move file="${build.dir}/@{install.name}.jar"
+      <move file="${build.maven}/@{install.name}.jar"
             tofile="@{install.dir}/@{install.name}.jar"/>
-      <move file="${build.dir}/@{install.name}-sources.jar"
+      <move file="${build.maven}/@{install.name}-sources.jar"
             tofile="@{install.dir}/@{install.name}-sources.jar"/>
 
       <checksum algorithm="SHA1" fileext=".sha1">
@@ -125,32 +138,32 @@
     </sequential>
   </macrodef>
 
-  <!--
-       Bundles staged artifact to release to ibiblio
-  -->
+  <!-- Stage (in ${build.maven}) and bundle an artifact in ${pubs.dir} for release -->
   <macrodef name="m2-bundle">
     <attribute name="artifactId"/>
     <attribute name="version"/>
     <sequential>
       <stage artifactId="@{artifactId}" version="@{version}"/>
-
-      <jar jarfile="${dist.base}/@{artifactId}-@{version}-bundle.jar">
-        <fileset dir="${build.dir}"/>
+      <jar jarfile="${pubs.dir}/@{artifactId}-@{version}-bundle.jar">
+        <fileset dir="${build.maven}"/>
       </jar>
     </sequential>
   </macrodef>
 
-  <!--
-       Performs Maven build staging
-  -->
+  <!-- Stage artefacts for Maven (copy to a clean ${build.maven} adding version). -->
   <macrodef name="stage">
     <attribute name="artifactId"/>
     <attribute name="version"/>
+    <attribute name="fromDir" default="${dist.dir}"/>
     <sequential>
-      <delete dir="${build.dir}"/>
-      <mkdir dir="${build.dir}"/>
+
+      <!-- Clean the temporary directory where we "stage" the primary and secondary artefacts. -->
+      <delete dir="${build.maven}"/>
+      <mkdir dir="${build.maven}"/>
+
+      <!-- Create the POM from the given template with placeholders replaced. -->
       <copy file="maven/pom-template.xml"
-            tofile="${build.dir}/@{artifactId}-@{version}.pom">
+            tofile="${build.maven}/@{artifactId}-@{version}.pom">
         <filterset>
           <filter token="PROJECT-VERSION" value="@{version}"/>
           <filter token="ARTIFACT-ID" value="@{artifactId}"/>
@@ -159,37 +172,17 @@
 
       <!-- XXX: License is already in the jars and maven central is
                 rejecting this in the bundle - investigate.
-
-      <copy file="LICENSE.txt" tofile="${build.dir}/LICENSE.txt"/>
+      <copy file="LICENSE.txt" tofile="${build.maven}/LICENSE.txt"/>
       -->
 
-      <copy file="${dist.base}/@{artifactId}.jar"
-            tofile="${build.dir}/@{artifactId}-@{version}.jar"/>
-
-      <copy file="dist/sources.jar" tofile="${build.dir}/@{artifactId}-@{version}-sources.jar"/>
-      <copy file="dist/javadoc.jar" tofile="${build.dir}/@{artifactId}-@{version}-javadoc.jar"/>
-
-      <!-- TODO: make the gpg sign step optional (disabled as not cross-platform)
-      <exec executable="gpg">
-          <arg value="-ab"/>
-          <arg value="${build.dir}/@{artifactId}-@{version}.jar"/>
-      </exec>
+      <!-- Copy over the primary artefact from @{fromDir} to the staging area. -->
+      <copy file="@{fromDir}/@{artifactId}.jar"
+            tofile="${build.maven}/@{artifactId}-@{version}.jar"/>
 
-      <exec executable="gpg">
-          <arg value="-ab"/>
-          <arg value="${build.dir}/@{artifactId}-@{version}-javadoc.jar"/>
-      </exec>
+      <!-- The source and Javadoc are the same each time (but add the version to the name). -->
+      <copy file="@{fromDir}/sources.jar" tofile="${build.maven}/@{artifactId}-@{version}-sources.jar"/>
+      <copy file="@{fromDir}/javadoc.jar" tofile="${build.maven}/@{artifactId}-@{version}-javadoc.jar"/>
 
-      <exec executable="gpg">
-          <arg value="-ab"/>
-          <arg value="${build.dir}/@{artifactId}-@{version}.pom"/>
-      </exec>
-
-      <exec executable="gpg">
-          <arg value="-ab"/>
-          <arg value="${build.dir}/@{artifactId}-@{version}-sources.jar"/>
-      </exec>
-      -->
     </sequential>
   </macrodef>
 
@@ -214,5 +207,13 @@
     <ant antfile="build.xml" target="all-jars"/>
   </target>
 
-  <target name="prepare" depends="build-jython, validate-pom, version"/>
+  <target name="prepare" depends="build-jython, validate-pom, version">
+    <local name="artefacts"/>
+    <property name="artefacts"
+              value="${gradle.repo}/${m2.groupDir}/jython-slim/${jython.version}" />
+    <condition property="gradle.published">
+      <available file="${artefacts}" type="dir"/>
+    </condition>
+  </target>
+
 </project>
diff --git a/settings.gradle b/settings.gradle
--- a/settings.gradle
+++ b/settings.gradle
@@ -2,4 +2,4 @@
  * Gradle settings for Jython. See also build.gradle.
  */
 
-rootProject.name = 'jython'
+rootProject.name = 'jython-slim'

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list