Use Gradle-version specific source sets

Instead of using Gradle-version specific compilation options, use distinct source sets. This also allows compilation of buildSrc/build-tools under IDEs that
don't understand the version-specific compilation options.

Relates to #22669
This commit is contained in:
Yannick Welsch 2017-01-24 11:03:07 +01:00
parent 36198e0275
commit 12b6ff5233
10 changed files with 167 additions and 39 deletions

View File

@ -95,15 +95,16 @@ dependencies {
// Gradle version-specific options (allows build to run with Gradle 2.13 as well as 2.14+/3.+) // Gradle version-specific options (allows build to run with Gradle 2.13 as well as 2.14+/3.+)
if (GradleVersion.current() == GradleVersion.version("2.13")) { if (GradleVersion.current() == GradleVersion.version("2.13")) {
// ProgressLogger(-Factory) classes are part of the public Gradle API // ProgressLogger(-Factory) classes are part of the public Gradle API
// Add default imports for (org.gradle.logging) sourceSets.main.groovy.srcDir 'src/main/gradle-2.13-groovy'
compileGroovy.groovyOptions.configurationScript = file('src/compile/groovy/gradle-2.13-loggers.groovy')
dependencies { dependencies {
compile 'ru.vyarus:gradle-animalsniffer-plugin:1.0.1' // last version compatible with Gradle 2.13 compile 'ru.vyarus:gradle-animalsniffer-plugin:1.0.1' // last version compatible with Gradle 2.13
} }
} else { } else {
// Gradle 2.14+ removed ProgressLogger(-Factory) classes from the public APIs // Gradle 2.14+ removed ProgressLogger(-Factory) classes from the public APIs
// Use logging dependency and add default imports for "org.gradle.internal.logging.progress" // Use logging dependency instead
compileGroovy.groovyOptions.configurationScript = file('src/compile/groovy/gradle-2.14-loggers.groovy') sourceSets.main.groovy.srcDir 'src/main/gradle-2.14-groovy'
dependencies { dependencies {
compileOnly "org.gradle:gradle-logging:${GradleVersion.current().getVersion()}" compileOnly "org.gradle:gradle-logging:${GradleVersion.current().getVersion()}"
compile 'ru.vyarus:gradle-animalsniffer-plugin:1.2.0' // Gradle 2.14 requires a version > 1.0.1 compile 'ru.vyarus:gradle-animalsniffer-plugin:1.2.0' // Gradle 2.14 requires a version > 1.0.1

View File

@ -0,0 +1,35 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
package org.elasticsearch.gradle
import org.gradle.logging.ProgressLoggerFactory
import javax.inject.Inject
/**
* Allows to inject a ProgressLoggerFactory to tasks in src/main/groovy
* without requiring the corresponding import of ProgressLoggerFactory,
* making it compatible with both Gradle 2.13 and 2.14+.
*/
trait ProgressLoggerFactoryInjection {
@Inject
ProgressLoggerFactory getProgressLoggerFactory() {
throw new UnsupportedOperationException()
}
}

View File

@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
package org.elasticsearch.gradle
import org.gradle.logging.ProgressLogger
/**
* Wraps a ProgressLogger so that code in src/main/groovy does not need to
* define imports on Gradle 2.13/2.14+ ProgressLoggers
*/
class ProgressLoggerWrapper {
ProgressLogger progressLogger
ProgressLoggerWrapper(ProgressLogger progressLogger) {
this.progressLogger = progressLogger
}
}

View File

@ -0,0 +1,35 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
package org.elasticsearch.gradle
import org.gradle.internal.logging.progress.ProgressLoggerFactory
import javax.inject.Inject
/**
* Allows to inject a ProgressLoggerFactory to tasks in src/main/groovy
* without requiring the corresponding import of ProgressLoggerFactory,
* making it compatible with both Gradle 2.13 and 2.14+.
*/
trait ProgressLoggerFactoryInjection {
@Inject
ProgressLoggerFactory getProgressLoggerFactory() {
throw new UnsupportedOperationException()
}
}

View File

@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
package org.elasticsearch.gradle
import org.gradle.internal.logging.progress.ProgressLogger
/**
* Wraps a ProgressLogger so that code in src/main/groovy does not need to
* define imports on Gradle 2.13/2.14+ ProgressLoggers
*/
class ProgressLoggerWrapper {
ProgressLogger progressLogger
ProgressLoggerWrapper(ProgressLogger progressLogger) {
this.progressLogger = progressLogger
}
}

View File

@ -8,6 +8,7 @@ import org.apache.tools.ant.BuildException
import org.apache.tools.ant.DefaultLogger import org.apache.tools.ant.DefaultLogger
import org.apache.tools.ant.RuntimeConfigurable import org.apache.tools.ant.RuntimeConfigurable
import org.apache.tools.ant.UnknownElement import org.apache.tools.ant.UnknownElement
import org.elasticsearch.gradle.ProgressLoggerFactoryInjection
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTreeElement import org.gradle.api.file.FileTreeElement
@ -21,9 +22,7 @@ import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.api.tasks.util.PatternSet import org.gradle.api.tasks.util.PatternSet
import org.gradle.util.ConfigureUtil import org.gradle.util.ConfigureUtil
import javax.inject.Inject class RandomizedTestingTask extends DefaultTask implements ProgressLoggerFactoryInjection {
class RandomizedTestingTask extends DefaultTask {
// TODO: change to "executable" to match gradle test params? // TODO: change to "executable" to match gradle test params?
@Optional @Optional
@ -89,11 +88,6 @@ class RandomizedTestingTask extends DefaultTask {
listenersConfig.listeners.add(new TestReportLogger(logger: logger, config: testLoggingConfig)) listenersConfig.listeners.add(new TestReportLogger(logger: logger, config: testLoggingConfig))
} }
@Inject
ProgressLoggerFactory getProgressLoggerFactory() {
throw new UnsupportedOperationException();
}
void jvmArgs(Iterable<String> arguments) { void jvmArgs(Iterable<String> arguments) {
jvmArgs.addAll(arguments) jvmArgs.addAll(arguments)
} }

View File

@ -25,6 +25,7 @@ import com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedStartEvent
import com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedSuiteResultEvent import com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedSuiteResultEvent
import com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedTestResultEvent import com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedTestResultEvent
import com.carrotsearch.ant.tasks.junit4.listeners.AggregatedEventListener import com.carrotsearch.ant.tasks.junit4.listeners.AggregatedEventListener
import org.elasticsearch.gradle.ProgressLoggerWrapper
import static com.carrotsearch.ant.tasks.junit4.FormattingUtils.formatDurationInSeconds import static com.carrotsearch.ant.tasks.junit4.FormattingUtils.formatDurationInSeconds
import static com.carrotsearch.ant.tasks.junit4.events.aggregated.TestStatus.ERROR import static com.carrotsearch.ant.tasks.junit4.events.aggregated.TestStatus.ERROR
@ -50,9 +51,7 @@ import static java.lang.Math.max
* quick. * quick.
*/ */
class TestProgressLogger implements AggregatedEventListener { class TestProgressLogger implements AggregatedEventListener {
/** Factory to build a progress logger when testing starts */ ProgressLoggerWrapper progressLoggerWrapper
ProgressLoggerFactory factory
ProgressLogger progressLogger
int totalSuites int totalSuites
int totalSlaves int totalSlaves
@ -77,14 +76,17 @@ class TestProgressLogger implements AggregatedEventListener {
/* Note that we probably overuse volatile here but it isn't hurting us and /* Note that we probably overuse volatile here but it isn't hurting us and
lets us move things around without worying about breaking things. */ lets us move things around without worying about breaking things. */
TestProgressLogger(Map args) {
progressLoggerWrapper = new ProgressLoggerWrapper(args.factory.newOperation(TestProgressLogger))
progressLoggerWrapper.progressLogger.setDescription('Randomized test runner')
}
@Subscribe @Subscribe
void onStart(AggregatedStartEvent e) throws IOException { void onStart(AggregatedStartEvent e) throws IOException {
totalSuites = e.suiteCount totalSuites = e.suiteCount
totalSlaves = e.slaveCount totalSlaves = e.slaveCount
progressLogger = factory.newOperation(TestProgressLogger) progressLoggerWrapper.progressLogger.started()
progressLogger.setDescription('Randomized test runner') progressLoggerWrapper.progressLogger.progress(
progressLogger.started()
progressLogger.progress(
"Starting JUnit4 for ${totalSuites} suites on ${totalSlaves} jvms") "Starting JUnit4 for ${totalSuites} suites on ${totalSlaves} jvms")
suitesFormat = "%0${widthForTotal(totalSuites)}d" suitesFormat = "%0${widthForTotal(totalSuites)}d"
@ -176,7 +178,7 @@ class TestProgressLogger implements AggregatedEventListener {
log += "J${sprintf(slavesFormat, eventSlave)} " log += "J${sprintf(slavesFormat, eventSlave)} "
} }
log += "completed ${eventDescription}" log += "completed ${eventDescription}"
progressLogger.progress(log) progressLoggerWrapper.progressLogger.progress(log)
} }
private static int widthForTotal(int total) { private static int widthForTotal(int total) {

View File

@ -20,6 +20,7 @@ package org.elasticsearch.gradle.vagrant
import com.carrotsearch.gradle.junit4.LoggingOutputStream import com.carrotsearch.gradle.junit4.LoggingOutputStream
import groovy.transform.PackageScope import groovy.transform.PackageScope
import org.elasticsearch.gradle.ProgressLoggerWrapper
import org.gradle.api.GradleScriptException import org.gradle.api.GradleScriptException
import org.gradle.api.logging.Logger import org.gradle.api.logging.Logger
@ -36,7 +37,7 @@ import java.util.regex.Matcher
* entire TAP stream at once and won't parse it stream-wise. * entire TAP stream at once and won't parse it stream-wise.
*/ */
public class TapLoggerOutputStream extends LoggingOutputStream { public class TapLoggerOutputStream extends LoggingOutputStream {
private final ProgressLogger progressLogger private final ProgressLoggerWrapper progressLoggerWrapper
private boolean isStarted = false private boolean isStarted = false
private final Logger logger private final Logger logger
private int testsCompleted = 0 private int testsCompleted = 0
@ -47,14 +48,14 @@ public class TapLoggerOutputStream extends LoggingOutputStream {
TapLoggerOutputStream(Map args) { TapLoggerOutputStream(Map args) {
logger = args.logger logger = args.logger
progressLogger = args.factory.newOperation(VagrantLoggerOutputStream) progressLoggerWrapper = new ProgressLoggerWrapper(args.factory.newOperation(VagrantLoggerOutputStream))
progressLogger.setDescription("TAP output for `${args.command}`") progressLoggerWrapper.progressLogger.setDescription("TAP output for `${args.command}`")
} }
@Override @Override
public void flush() { public void flush() {
if (isStarted == false) { if (isStarted == false) {
progressLogger.started() progressLoggerWrapper.progressLogger.started()
isStarted = true isStarted = true
} }
if (end == start) return if (end == start) return
@ -103,7 +104,7 @@ public class TapLoggerOutputStream extends LoggingOutputStream {
String counts = sprintf(countsFormat, String counts = sprintf(countsFormat,
[testsCompleted, testsFailed, testsSkipped, testCount]) [testsCompleted, testsFailed, testsSkipped, testCount])
progressLogger.progress("Tests $counts, $status [$suiteName] $testName") progressLoggerWrapper.progressLogger.progress("Tests $counts, $status [$suiteName] $testName")
if (!success) { if (!success) {
logger.warn(line) logger.warn(line)
} }

View File

@ -19,16 +19,15 @@
package org.elasticsearch.gradle.vagrant package org.elasticsearch.gradle.vagrant
import org.apache.commons.io.output.TeeOutputStream import org.apache.commons.io.output.TeeOutputStream
import org.elasticsearch.gradle.ProgressLoggerFactoryInjection
import org.elasticsearch.gradle.LoggedExec import org.elasticsearch.gradle.LoggedExec
import org.gradle.api.tasks.Input import org.gradle.api.tasks.Input
import javax.inject.Inject
/** /**
* Runs a vagrant command. Pretty much like Exec task but with a nicer output * Runs a vagrant command. Pretty much like Exec task but with a nicer output
* formatter and defaults to `vagrant` as first part of commandLine. * formatter and defaults to `vagrant` as first part of commandLine.
*/ */
public class VagrantCommandTask extends LoggedExec { public class VagrantCommandTask extends LoggedExec implements ProgressLoggerFactoryInjection {
@Input @Input
String boxName String boxName
@ -56,9 +55,4 @@ public class VagrantCommandTask extends LoggedExec {
stuff starts with ==> $box */ stuff starts with ==> $box */
squashedPrefix: "==> $boxName: ") squashedPrefix: "==> $boxName: ")
} }
@Inject
ProgressLoggerFactory getProgressLoggerFactory() {
throw new UnsupportedOperationException();
}
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.gradle.vagrant package org.elasticsearch.gradle.vagrant
import com.carrotsearch.gradle.junit4.LoggingOutputStream import com.carrotsearch.gradle.junit4.LoggingOutputStream
import org.gradle.api.logging.Logger import org.elasticsearch.gradle.ProgressLoggerWrapper
/** /**
* Adapts an OutputStream being written to by vagrant into a ProcessLogger. It * Adapts an OutputStream being written to by vagrant into a ProcessLogger. It
@ -45,7 +45,7 @@ import org.gradle.api.logging.Logger
public class VagrantLoggerOutputStream extends LoggingOutputStream { public class VagrantLoggerOutputStream extends LoggingOutputStream {
private static final String HEADING_PREFIX = '==> ' private static final String HEADING_PREFIX = '==> '
private final ProgressLogger progressLogger private final ProgressLoggerWrapper progressLoggerWrapper
private boolean isStarted = false private boolean isStarted = false
private String squashedPrefix private String squashedPrefix
private String lastLine = '' private String lastLine = ''
@ -53,15 +53,15 @@ public class VagrantLoggerOutputStream extends LoggingOutputStream {
private String heading = '' private String heading = ''
VagrantLoggerOutputStream(Map args) { VagrantLoggerOutputStream(Map args) {
progressLogger = args.factory.newOperation(VagrantLoggerOutputStream) progressLoggerWrapper = new ProgressLoggerWrapper(args.factory.newOperation(VagrantLoggerOutputStream))
progressLogger.setDescription("Vagrant output for `$args.command`") progressLoggerWrapper.progressLogger.setDescription("Vagrant output for `$args.command`")
squashedPrefix = args.squashedPrefix squashedPrefix = args.squashedPrefix
} }
@Override @Override
public void flush() { public void flush() {
if (isStarted == false) { if (isStarted == false) {
progressLogger.started() progressLoggerWrapper.progressLogger.started()
isStarted = true isStarted = true
} }
if (end == start) return if (end == start) return
@ -96,6 +96,6 @@ public class VagrantLoggerOutputStream extends LoggingOutputStream {
} else { } else {
return return
} }
progressLogger.progress(line) progressLoggerWrapper.progressLogger.progress(line)
} }
} }