You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafu.apache.org by ey...@apache.org on 2018/07/08 10:45:46 UTC

datafu git commit: DATAFU-146 Upgrade to Gradle v4.8.1

Repository: datafu
Updated Branches:
  refs/heads/master 73989cfa6 -> 8dae166c0


DATAFU-146 Upgrade to Gradle v4.8.1

This updates to Gradle 4.8.1.  The only issue I encountered doing this was that autojar 1.0.1 is not compatible with this version of Gradle.  Only a small modification is required to fix it:

    manifest.writeTo(manifestFile.path)

The autojar plugin does not appear to be maintained anymore, so I have checked the code into our repo.  It is Apache 2 licensed so this should not be a problem.  Since we can't bundle the autojar-2.1.jar I have kept the dependency on gradle-autojar-1.0.1, as this has it bundled as a resource.  I modified ExtractAutojar in the plugin to account for this difference.

Signed-off-by: Eyal Allweil <ey...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/datafu/repo
Commit: http://git-wip-us.apache.org/repos/asf/datafu/commit/8dae166c
Tree: http://git-wip-us.apache.org/repos/asf/datafu/tree/8dae166c
Diff: http://git-wip-us.apache.org/repos/asf/datafu/diff/8dae166c

Branch: refs/heads/master
Commit: 8dae166c0f755a07a65cfa371f3da2d32993de65
Parents: 73989cf
Author: Matthew Hayes <mh...@apache.org>
Authored: Thu Jul 5 09:48:18 2018 -0700
Committer: Eyal Allweil <ey...@apache.org>
Committed: Sun Jul 8 13:45:10 2018 +0300

----------------------------------------------------------------------
 .gitignore                                      |   2 +-
 buildSrc/build.gradle                           |  27 +++
 .../datafu/autojar/GradleAutojarPlugin.groovy   |  65 ++++++
 .../groovy/datafu/autojar/task/Autojar.groovy   | 204 +++++++++++++++++++
 .../datafu/autojar/task/ExtractAutojar.groovy   |  58 ++++++
 datafu-pig/build.gradle                         |   8 +-
 gradle.properties                               |   2 +-
 7 files changed, 357 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index eebd04a..09885f6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
 /dist/
 *.release
-/.gradle/
+.gradle/
 build/
 annotation-plugin.jar
 .factorypath

http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/buildSrc/build.gradle
----------------------------------------------------------------------
diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle
new file mode 100644
index 0000000..cd09b54
--- /dev/null
+++ b/buildSrc/build.gradle
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+repositories {
+  mavenCentral()
+}
+
+dependencies {
+  compile 'com.github.rholder:gradle-autojar:1.0.1'
+}
+

http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/buildSrc/src/main/groovy/datafu/autojar/GradleAutojarPlugin.groovy
----------------------------------------------------------------------
diff --git a/buildSrc/src/main/groovy/datafu/autojar/GradleAutojarPlugin.groovy b/buildSrc/src/main/groovy/datafu/autojar/GradleAutojarPlugin.groovy
new file mode 100644
index 0000000..59bb84f
--- /dev/null
+++ b/buildSrc/src/main/groovy/datafu/autojar/GradleAutojarPlugin.groovy
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2013 Ray Holder
+ *
+ * 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.
+ */
+
+package datafu.autojar
+
+import datafu.autojar.task.Autojar
+import datafu.autojar.task.ExtractAutojar
+import org.gradle.api.Plugin
+import org.gradle.api.Project
+
+/**
+ * This plugin rolls up your current project's classes and all of its
+ * dependencies into a single flattened jar archive walking each dependency and
+ * including only those which are used.
+ *
+ * At a minimum, the configuration expects to find a custom 'mainClass' when
+ * adding the plugin to your own builds, as in:
+ *
+ * <pre>
+ * apply plugin: 'gradle-autojar'
+ *
+ * task awesomeFunJar(type: Autojar) {
+ *     mainClass = 'com.github.rholder.awesome.MyAwesomeMain'
+ * }
+ * </pre>
+ *
+ * You can also manually specify additional classes to walk that were otherwise
+ * not correctly detected by the plugin's dependency analysis because of
+ * reflection use, etc. as well as non-class resources, as in:
+ *
+ * <pre>
+ * apply plugin: 'gradle-autojar'
+ *
+ * task awesomeFunJar(type: Autojar) {
+ *     mainClass = 'com.github.rholder.awesome.MyAwesomeMain'
+ *     autojarFiles = ['foo.txt', 'dir/foo2.txt']
+ *     autojarClasses = ['org.apache.commons.lang.time.DateUtils']
+ * }
+ * </pre>
+ */
+class GradleAutojarPlugin implements Plugin<Project> {
+
+    void apply(Project project) {
+        project.apply(plugin:'java')
+
+        project.ext.autojarBuildDir = new File(project.buildDir, "autojar-build")
+        project.ext.autojarBuildDir.mkdirs()
+        project.ext.Autojar = Autojar.class
+
+        project.tasks.create('extractAutojar', ExtractAutojar.class)
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/buildSrc/src/main/groovy/datafu/autojar/task/Autojar.groovy
----------------------------------------------------------------------
diff --git a/buildSrc/src/main/groovy/datafu/autojar/task/Autojar.groovy b/buildSrc/src/main/groovy/datafu/autojar/task/Autojar.groovy
new file mode 100644
index 0000000..a620337
--- /dev/null
+++ b/buildSrc/src/main/groovy/datafu/autojar/task/Autojar.groovy
@@ -0,0 +1,204 @@
+/*
+ * Copyright 2013 Ray Holder
+ *
+ * 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.
+ */
+
+package datafu.autojar.task
+
+import org.gradle.api.InvalidUserDataException
+import org.gradle.api.Task
+import org.gradle.api.artifacts.Configuration
+import org.gradle.api.artifacts.PublishArtifact
+import org.gradle.api.java.archives.Manifest
+import org.gradle.api.java.archives.internal.DefaultManifest
+import org.gradle.api.logging.Logger
+import org.gradle.api.tasks.JavaExec
+import org.gradle.api.tasks.TaskAction
+import org.gradle.api.tasks.TaskDependency
+import org.gradle.api.tasks.bundling.Jar
+import org.gradle.util.ConfigureUtil
+
+/**
+ * This is the primary Task type used to create Autojar archives.
+ */
+class Autojar extends JavaExec implements PublishArtifact {
+
+    Logger logger
+    Jar baseJar
+    Manifest manifest
+    ExtractAutojar extractAutojar
+
+    Configuration targetConfiguration
+
+    String mainClass
+    List<String> autojarClasses    // convert these to raw files
+    List<String> autojarFiles      // all the class files, etc.
+
+    File autojarBuildDir
+    String autojarExtra     // default to -bav
+    String autojarClasspath // -c all dependencies, including base jar
+    String autojarManifest  // path to final manifest file -m
+    File autojarOutput      // -o path to output file
+
+    // publish artifact overrides
+    Date publishDate
+    String publishClassifier
+    String publishType
+    String publishExtension
+
+    Autojar() {
+        group = "Autojar"
+        description = "Create an Autojar runnable archive from the current project using a given main class."
+
+        logger = project.logger
+
+        autojarBuildDir = project.ext.autojarBuildDir
+        extractAutojar = project.tasks.extractAutojar
+
+        // default to main project jar
+        baseJar = project.tasks.jar
+
+        autojarOutput = new File(autojarBuildDir, generateFilename(baseJar, "autojar"))
+
+        dependsOn = [baseJar, extractAutojar]
+        inputs.files([baseJar.getArchivePath().absoluteFile, extractAutojar.extractedFile])
+        outputs.file(autojarOutput)
+    }
+
+    @TaskAction
+    @Override
+    public void exec() {
+        setMain('org.sourceforge.autojar.Autojar')
+        classpath(extractAutojar.extractedFile.absolutePath)
+
+        // munge the autojarClasspath
+        autojarClasspath = baseJar.getArchivePath().absolutePath
+
+        // default to runtime configuration if none is specified
+        targetConfiguration = targetConfiguration ?: project.configurations.runtime
+        def libs = targetConfiguration.resolve()
+        libs.each {
+            logger.debug("Including dependency: " + it.absolutePath)
+            autojarClasspath += ":" + it.absolutePath
+        }
+
+        // default to -ba if none is specified
+        autojarExtra = autojarExtra ?: "-ba"
+
+        autojarFiles = autojarFiles ?: []
+
+        // convert class notation to raw files for Autojar
+        autojarClasses = autojarClasses ?: []
+        autojarClasses.each {
+            autojarFiles.add(it.replaceAll("\\.", "/") + ".class")
+        }
+
+        // default to whatever was in the manifest closure
+        manifest = manifest ?: new DefaultManifest(project.fileResolver)
+
+        // infer main class starting point from manifest if it exists
+        if(mainClass) {
+            autojarFiles.add(mainClass.replaceAll("\\.", "/") + ".class")
+            manifest.attributes.put('Main-Class', mainClass)
+        }
+
+        // by now we should have at least one file
+        if(!autojarFiles) {
+            throw new InvalidUserDataException("No files set in autojarFiles and no main class to infer.")
+        }
+
+        autojarManifest = writeJarManifestFile(manifest).absolutePath
+
+        def autojarArgs = [autojarExtra, "-c", autojarClasspath, "-m", autojarManifest,"-o", autojarOutput]
+        autojarArgs.addAll(autojarFiles)
+        logger.info('{}', autojarArgs)
+
+        args(autojarArgs)
+        super.exec()
+    }
+
+    @Override
+    String getExtension() {
+        return publishExtension ?: Jar.DEFAULT_EXTENSION
+    }
+
+    @Override
+    String getType() {
+        return publishType ?: Jar.DEFAULT_EXTENSION
+    }
+
+    @Override
+    String getClassifier() {
+        return publishClassifier ?: 'autojar'
+    }
+
+    @Override
+    File getFile() {
+        return autojarOutput
+    }
+
+    @Override
+    Date getDate() {
+        return publishDate ?: new Date(autojarOutput.lastModified())
+    }
+
+    @Override
+    TaskDependency getBuildDependencies() {
+        // we have to build ourself, since we're a Task
+        Task thisTask = this
+        return new TaskDependency() {
+            @Override
+            Set<? extends Task> getDependencies(Task task) {
+                return [thisTask] as Set
+            }
+        }
+    }
+
+    /**
+     * Allow configuration view of a 'manifest' closure similar to the Jar task.
+     *
+     * @param configureClosure target closure
+     */
+    Autojar manifest(Closure configureClosure) {
+        manifest = manifest ?: new DefaultManifest(project.fileResolver)
+        ConfigureUtil.configure(configureClosure, manifest);
+        return this;
+    }
+
+    /**
+     * Return a manifest configured to boot the jar using One-JAR and then
+     * passing over control to the configured main class.
+     */
+    static File writeJarManifestFile(Manifest manifest) {
+        File manifestFile = File.createTempFile("manifest", ".mf")
+        manifestFile.deleteOnExit()
+
+        manifest.writeTo(manifestFile.path)
+
+        // hack to clip off the first line, Manifest-Version, in the file because Autojar is weird
+        def lines = manifestFile.readLines()
+        lines.remove(0)
+        manifestFile.setText(lines.join("\n"))
+
+        return manifestFile
+    }
+
+    /**
+     * This is kind of a hack to ensure we get "-classifier.jar" tacked on to
+     * archiveName + a valid version.
+     */
+    static String generateFilename(Jar jar, String classifier) {
+        return jar.archiveName - ("." + jar.extension) + "-" + classifier + "." + jar.extension
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/buildSrc/src/main/groovy/datafu/autojar/task/ExtractAutojar.groovy
----------------------------------------------------------------------
diff --git a/buildSrc/src/main/groovy/datafu/autojar/task/ExtractAutojar.groovy b/buildSrc/src/main/groovy/datafu/autojar/task/ExtractAutojar.groovy
new file mode 100644
index 0000000..60f3f3c
--- /dev/null
+++ b/buildSrc/src/main/groovy/datafu/autojar/task/ExtractAutojar.groovy
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2013 Ray Holder
+ *
+ * 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.
+ */
+
+package datafu.autojar.task
+
+import org.gradle.api.GradleException
+
+import org.gradle.api.DefaultTask
+import org.gradle.api.tasks.TaskAction
+
+/**
+ * Extract the bundled binary autojar package and prepare it to be forked and
+ * executed separately.
+ */
+class ExtractAutojar extends DefaultTask {
+
+    File extractedFile
+
+    ExtractAutojar() {
+        group = "Autojar"
+        description = "Extract the runnable Autojar archive to the build directory."
+
+        extractedFile = new File(project.ext.autojarBuildDir, 'autojar-2.1.jar')
+
+        dependsOn = [project.tasks.jar]
+        outputs.file(extractedFile)
+    }
+
+    @TaskAction
+    def extract() {
+        logger.info('Extracting Autojar archive to: {}', extractedFile)
+        // pull resource out of the classpath and write a copy of it
+        extractedFile.parentFile.mkdirs()
+        def jarResource = "/autojar-2.1.jar"
+        def instance = this.getClass().getClassLoader().loadClass('com.github.rholder.gradle.autojar.task.ExtractAutojar')
+        def resourceStream = instance.getResourceAsStream(jarResource)
+        if (resourceStream == null) {
+            throw new GradleException("Failed to load resouce " + jarResource);
+        }
+        extractedFile.withOutputStream { os ->
+            os << resourceStream
+        }
+        logger.info('Extracted Autojar archive to: {}, {} bytes', extractedFile, extractedFile.length())
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/datafu-pig/build.gradle
----------------------------------------------------------------------
diff --git a/datafu-pig/build.gradle b/datafu-pig/build.gradle
index c0531b0..579a475 100644
--- a/datafu-pig/build.gradle
+++ b/datafu-pig/build.gradle
@@ -21,12 +21,6 @@ buildscript {
   repositories {
       mavenCentral()
   }
-
-  // autojar only works with gradle versions < 4.0
-  // we need to fix it or find a different solution in order to upgrade gradle
-  dependencies {
-      classpath 'com.github.rholder:gradle-autojar:1.0.1'
-  }
 }
 
 plugins {
@@ -34,7 +28,7 @@ plugins {
 }
 
 apply plugin: 'java'
-apply plugin: 'gradle-autojar'
+apply plugin: datafu.autojar.GradleAutojarPlugin
 
 archivesBaseName = 'datafu-pig'
 

http://git-wip-us.apache.org/repos/asf/datafu/blob/8dae166c/gradle.properties
----------------------------------------------------------------------
diff --git a/gradle.properties b/gradle.properties
index 8f5cd99..aa953be 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -17,6 +17,6 @@
 
 group=org.apache.datafu
 version=1.4.0
-gradleVersion=3.5.1
+gradleVersion=4.8.1
 org.gradle.jvmargs="-XX:MaxPermSize=512m"
 release=false
\ No newline at end of file