Skip to content

Commit

Permalink
Merge pull request #154 from pivotal-jbarrett/feature/uber-jar
Browse files Browse the repository at this point in the history
Build an über jar using the standard jar plugin.
  • Loading branch information
melix authored Aug 2, 2019
2 parents 3f5003d + 6482f72 commit 032767b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 114 deletions.
16 changes: 8 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ val shadowVersion: String by project
val jacocoVersion: String by project

dependencies {
"compile"(localGroovy())
"compile"(gradleApi())
"compile"("org.openjdk.jmh:jmh-core:$jmhVersion")
"implementation"(localGroovy())
"implementation"(gradleApi())
"implementation"("org.openjdk.jmh:jmh-core:$jmhVersion")
"compileOnly"("org.openjdk.jmh:jmh-generator-bytecode:$jmhVersion")

"testCompile"("junit:junit:$junitVersion")
testCompile("org.spockframework:spock-core:$spockVersion") {
"testImplementation"("junit:junit:$junitVersion")
testImplementation("org.spockframework:spock-core:$spockVersion") {
exclude(mapOf("group" to "org.codehaus.groovy", "module" to "groovy-all"))
}
"testCompile"("com.github.jengelman.gradle.plugins:shadow:$shadowVersion")
"testImplementation"("com.github.jengelman.gradle.plugins:shadow:$shadowVersion")

"testCompile"("org.openjdk.jmh:jmh-core:$jmhVersion")
"testCompile"("org.openjdk.jmh:jmh-generator-bytecode:$jmhVersion")
"testImplementation"("org.openjdk.jmh:jmh-core:$jmhVersion")
"testImplementation"("org.openjdk.jmh:jmh-generator-bytecode:$jmhVersion")
}

tasks {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ project_vcs=https://github.com/melix/jmh-gradle-plugin.git

jacocoVersion = 0.8.4
jmhVersion = 1.21
shadowVersion = 5.0.0
shadowVersion = 5.1.0
junitVersion = 4.12
spockVersion = 1.2-groovy-2.4
132 changes: 66 additions & 66 deletions gradle/funcTest.gradle
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
/*
* Copyright 2014-2017 the original author or authors.
*
* 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.
*/

sourceSets {
functionalTest {
groovy.srcDir file('src/funcTest/groovy')
resources.srcDir file('src/funcTest/resources')
compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
}
}

task createClasspathManifest {
def outputDir = sourceSets.functionalTest.output.resourcesDir

inputs.files sourceSets.test.runtimeClasspath
outputs.dir outputDir

doLast {
outputDir.mkdirs()
file("$outputDir/plugin-classpath.txt").text = files(sourceSets.test.runtimeClasspath, jar.archivePath).join("\n")
}
}

dependencies {
functionalTestCompile gradleTestKit()
functionalTestCompile files(createClasspathManifest)
}

compileFunctionalTestJava.dependsOn createClasspathManifest

task functionalTest(type: Test, dependsOn: [jar]) {
description = 'Runs the functional tests.'
group = 'verification'
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
mustRunAfter test

reports {
html.destination = project.file("$html.destination/functional")
junitXml.destination = project.file("$junitXml.destination/functional")
}
}

check.dependsOn functionalTest

idea.module {
sourceSets.functionalTest.allSource.srcDirs.each {
testSourceDirs += it
}

scopes.TEST.plus += [configurations.functionalTestCompile]
scopes.TEST.plus += [configurations.functionalTestRuntime]
/*
* Copyright 2014-2017 the original author or authors.
*
* 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.
*/

sourceSets {
functionalTest {
groovy.srcDir file('src/funcTest/groovy')
resources.srcDir file('src/funcTest/resources')
compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
}
}

task createClasspathManifest {
def outputDir = sourceSets.functionalTest.output.resourcesDir

inputs.files sourceSets.test.runtimeClasspath
outputs.dir outputDir

doLast {
outputDir.mkdirs()
file("$outputDir/plugin-classpath.txt").text = files(sourceSets.test.runtimeClasspath, jar.archivePath).join("\n")
}
}

dependencies {
functionalTestImplementation gradleTestKit()
functionalTestImplementation files(createClasspathManifest)
}

compileFunctionalTestJava.dependsOn createClasspathManifest

task functionalTest(type: Test, dependsOn: [jar]) {
description = 'Runs the functional tests.'
group = 'verification'
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
mustRunAfter test

reports {
html.destination = project.file("$html.destination/functional")
junitXml.destination = project.file("$junitXml.destination/functional")
}
}

check.dependsOn functionalTest

idea.module {
sourceSets.functionalTest.allSource.srcDirs.each {
testSourceDirs += it
}

scopes.TEST.plus += [configurations.functionalTestCompileClasspath]
scopes.TEST.plus += [configurations.functionalTestRuntimeClasspath]
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-rc-1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class ProjectWithDuplicateDependenciesSpec extends Specification {
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.apache.commons:commons-lang3:3.2'
implementation 'org.apache.commons:commons-lang3:3.0.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.apache.commons:commons-lang3:3.2'
jmh 'org.apache.commons:commons-lang3:3.4'
}
Expand Down
2 changes: 1 addition & 1 deletion src/funcTest/resources/groovy-project/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
implementation 'org.codehaus.groovy:groovy-all:2.4.12'
}

jmh {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ repositories {
}

dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.apache.commons:commons-lang3:3.4'
testImplementation 'junit:junit:4.12'
testImplementation 'org.apache.commons:commons-lang3:3.4'
}

jmh {
Expand Down
2 changes: 1 addition & 1 deletion src/funcTest/resources/kotlin-project/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repositories {
}

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.2.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.2.0'
}

jmh {
Expand Down
21 changes: 13 additions & 8 deletions src/main/groovy/me/champeau/gradle/JMHPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvableDependencies
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.file.FileCopyDetails
import org.gradle.api.invocation.Gradle
Expand All @@ -40,7 +39,7 @@ import java.util.concurrent.atomic.AtomicReference
* Configures the JMH Plugin.
*/
class JMHPlugin implements Plugin<Project> {
private static boolean IS_GRADLE_MIN_55 = GradleVersion.current().compareTo(GradleVersion.version("5.5-rc-1")) >= 0
private static boolean IS_GRADLE_MIN_55 = GradleVersion.current().compareTo(GradleVersion.version("5.5.0")) >= 0

public static final String JMH_CORE_DEPENDENCY = 'org.openjdk.jmh:jmh-core:'
public static final String JMH_GENERATOR_DEPENDENCY = 'org.openjdk.jmh:jmh-generator-bytecode:'
Expand Down Expand Up @@ -80,7 +79,7 @@ class JMHPlugin implements Plugin<Project> {

createJmhCompileGeneratedClassesTask(project, jmhGeneratedSourcesDir, jmhGeneratedClassesDir, extension)

def metaInfExcludes = ['META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA']
def metaInfExcludes = ['module-info.class', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA']
if (hasShadow) {
createShadowJmhJar(project, extension, jmhGeneratedResourcesDir, jmhGeneratedClassesDir, metaInfExcludes, runtimeConfiguration)
} else {
Expand Down Expand Up @@ -227,9 +226,15 @@ class JMHPlugin implements Plugin<Project> {
it.group JMH_GROUP
it.dependsOn JMH_TASK_COMPILE_GENERATED_CLASSES_NAME
it.inputs.files project.sourceSets.jmh.output
it.from(runtimeConfiguration) {
exclude metaInfExcludes
it.inputs.files project.sourceSets.main.output
if (extension.includeTests) {
it.inputs.files project.sourceSets.test.output
}
it.from {
runtimeConfiguration.asFileTree.collect { File f ->
f.isDirectory() ? f : project.zipTree(f)
}
}.exclude(metaInfExcludes)
it.doFirst {
from(project.sourceSets.jmh.output)
from(project.sourceSets.main.output)
Expand All @@ -249,7 +254,7 @@ class JMHPlugin implements Plugin<Project> {
attributes 'Main-Class': 'org.openjdk.jmh.Main'
}

it.classifier = JMH_NAME
it.archiveClassifier = JMH_NAME
}
}

Expand Down Expand Up @@ -280,10 +285,10 @@ class JMHPlugin implements Plugin<Project> {
newConfig.setCanBeResolved(true)
newConfig.setVisible(false)
newConfig.extendsFrom(project.configurations.getByName('jmh'))
newConfig.extendsFrom(project.configurations.getByName('runtimeOnly'))
newConfig.extendsFrom(project.configurations.getByName('runtimeClasspath'))
project.afterEvaluate {
if (extension.includeTests) {
newConfig.extendsFrom(project.configurations.getByName('testRuntimeOnly'))
newConfig.extendsFrom(project.configurations.getByName('testRuntimeClasspath'))
}
}
newConfig
Expand Down
40 changes: 17 additions & 23 deletions src/main/groovy/me/champeau/gradle/JMHTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@
*/
package me.champeau.gradle;

import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.tasks.Jar;
import org.gradle.workers.IsolationMode;
import org.gradle.workers.WorkerConfiguration;
import org.gradle.workers.WorkerExecutor;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import javax.inject.Inject;
import java.io.File;
import java.util.Arrays;

/**
* The JMH task converts our {@link JMHPluginExtension extension configuration} into JMH specific
Expand All @@ -53,23 +50,20 @@ public void before() {
final Options options = extension.resolveArgs();
extension.getResultsFile().getParentFile().mkdirs();

workerExecutor.submit(IsolatedRunner.class, new Action<WorkerConfiguration>() {
@Override
public void execute(final WorkerConfiguration workerConfiguration) {
workerConfiguration.setIsolationMode(IsolationMode.PROCESS);
ConfigurationContainer configurations = getProject().getConfigurations();
FileCollection classpath = configurations.getByName("jmh").plus(getProject().files(getJarArchive()));
if (extension.isIncludeTests()) {
classpath = classpath.plus(configurations.getByName("testRuntimeClasspath"));
}
// TODO: This isn't quite right. JMH is already a part of the worker classpath,
// but we need it to be part of the "classpath under test" too.
// We only need the jar for the benchmarks on the classpath so that the BenchmarkList resource reader
// can find the BenchmarkList file in the jar.
workerConfiguration.classpath(classpath);
workerConfiguration.params(options, classpath.getFiles());
workerConfiguration.getForkOptions().getSystemProperties().put(JAVA_IO_TMPDIR, getTemporaryDir());
workerExecutor.submit(IsolatedRunner.class, workerConfiguration -> {
workerConfiguration.setIsolationMode(IsolationMode.PROCESS);
ConfigurationContainer configurations = getProject().getConfigurations();
FileCollection classpath = configurations.getByName("jmh").plus(getProject().files(getJarArchive()));
if (extension.isIncludeTests()) {
classpath = classpath.plus(configurations.getByName("testRuntimeClasspath"));
}
// TODO: This isn't quite right. JMH is already a part of the worker classpath,
// but we need it to be part of the "classpath under test" too.
// We only need the jar for the benchmarks on the classpath so that the BenchmarkList resource reader
// can find the BenchmarkList file in the jar.
workerConfiguration.classpath(classpath);
workerConfiguration.params(options, classpath.getFiles());
workerConfiguration.getForkOptions().getSystemProperties().put(JAVA_IO_TMPDIR, getTemporaryDir());
});
}

Expand All @@ -78,8 +72,8 @@ public void setDidWork(final boolean didWork) {
super.setDidWork(didWork);
}

private File getJarArchive() {
return ((Jar)getProject().getTasks().getByName(JMHPlugin.JMH_JAR_TASK_NAME)).getArchivePath();
private Provider<RegularFile> getJarArchive() {
return ((Jar)getProject().getTasks().getByName(JMHPlugin.JMH_JAR_TASK_NAME)).getArchiveFile();
}

}

0 comments on commit 032767b

Please sign in to comment.