I am trying to create gradle tasks to run only a sub set of tests based on annotations, which is detailed in the gradle dsl. I am trying to set the testInstrumentationRunnerArguments during the gradle execution phase. While I am successfully setting the arguments it doesn't appear to actually do any filtering.
We do have the tests located in a different source set path but we have added them to the default androidTest source set. The tests all run they are simply not filtered.
My questions then become: Can the test arguments be dynamically set during the execution phase of gradle or do they need to be set in the configuration phase? If they can be set during execution can any one point out why the below gradle wont work?
Here is my test.gradle which is applied after the android library configuration closure.
/*
* Copyright (c) 2018 company. All rights reserved.
*/
ext {
integrationTest = false
networkIntegrationTest = false
}
android.sourceSets {
androidTest {
java {
// add our custom test source sets so android studio gives us good tooling
srcDirs += './src/integrationNetworkTest/java'
srcDirs += './src/integrationTest/java'
}
res {
srcDirs += './src/integrationNetworkTest/res'
srcDirs += './src/integrationTest/res'
}
}
}
tasks.whenTaskAdded { task ->
def name = task.name.toLowerCase()
if(name.contains('connectedcheck') || task.name.matches("connected([a-zA-Z]?)+AndroidTest")) {
task.doFirst {
if(!integrationTest && !networkIntegrationTest) {
android.productFlavors.each {
it.testInstrumentationRunnerArguments.put('notAnnotation',
'com.company.android.infrastructure.test.IntegrationTest,com.company.android.infrastructure.test.IntegrationNetworkTest')
}
}
else {
StringBuilder sb = new StringBuilder()
if(integrationTest && networkIntegrationTest) {
sb.append('com.company.android.infrastructure.test.IntegrationTest')
sb.append(',')
sb.append('com.company.android.infrastructure.test.IntegrationNetworkTest')
}
else if (integrationTest) {
sb.append('com.company.android.infrastructure.test.IntegrationTest')
}
else if (networkIntegrationTest) {
sb.append('com.company.android.infrastructure.test.IntegrationNetworkTest')
}
if(integrationTest || networkIntegrationTest) {
android.productFlavors.each {
it.testInstrumentationRunnerArguments.put('annotation', sb.toString())
}
}
}
android.productFlavors.each {
println "${it.name} Test Annotations: ${it.testInstrumentationRunnerArguments}"
}
}
}
}
project.afterEvaluate {
android.libraryVariants.each { variant ->
tasks.create("${variant.name}IntegrationTest") {
group 'company'
description 'Run company Integration tests with only network requests mocked'
finalizedBy "connected${variant.name.capitalize()}AndroidTest"
doLast {
integrationTest = true
}
}
tasks.create("${variant.name}IntegrationNetworkTest") {
group 'company'
description 'Run company Integration tests with real network requests'
finalizedBy "connected${variant.name.capitalize()}AndroidTest"
doLast {
networkIntegrationTest = true
}
}
}
}
Gradle: 4.1
Android Plugin: 3.0.1
*Edit: * I think its a bug: Android Bug Tracker
Related
I'm trying to use ProGuard to minimize my gradle plugin and it works well, producing the output file a-min.jar
However, com.gradle.plugin-publish plugin just doesn't recognize it when using task publishPlugins, it tells
Cannot determine main artifact to upload - could not find jar artifact with empty classifier
Strangely, when using publishToMavenLocal it did work well.
I've tried some tricks to replace the main jar with my processed jar, but always failed. Is there any new sight here?
afterEvaluate {
publishing {
publications {
withType<MavenPublication> {
if (name == "pluginMaven") {
setArtifacts(listOf(
artifact(minJarPath){
classifier = ""
extension = "jar"
}
))
}
}
}
}
}
I've tried to decompile com.gradle.plugin-publish:0.18.0, getting that
private File findMainArtifact() {
if (this.useAutomatedPublishing) {
for (UsageContext variant : this.javaRuntimeVariants) {
for (PublishArtifact artifact : variant.getArtifacts()) {
if (Util.isBlank(artifact.getClassifier()) && "jar".equals(artifact.getExtension()))
return artifact.getFile();
}
}
} else {
Configuration archivesConfiguration = getProject().getConfigurations().getByName("archives");
for (PublishArtifact artifact : archivesConfiguration.getAllArtifacts()) {
if (Util.isBlank(artifact.getClassifier()) && "jar".equals(artifact.getExtension()))
return artifact.getFile();
}
}
throw new IllegalArgumentException("Cannot determine main artifact to upload - could not find jar artifact with empty classifier");
}
But I don't know how to add a artifact to "AllArtifacts".
Full code: GitHub#ArcticLampyrid/gradle-git-version
Resolved by some tricks although it is not elegant.
Solution
Create a real jar task to include proguard outputs and add it to outgoing of apiElements & runtimeElements.
Delete the unprocessed jar from apiElements & runtimeElements. (for publishPlugins task)
Also, modify publications (for publishToMavenLocal task)
Example
/* Written in Kotlin DSL for Gradle */
/* build.gradle.kts */
// for `publishPlugins` task
val minJar by tasks.creating(Jar::class){
dependsOn(genMinJar)
from(zipTree(minJarPath))
}
jar {
archiveClassifier.set("unpacked")
}
configurations {
artifacts {
arrayOf(apiElements, runtimeElements).forEach {
it.get().outgoing.artifacts.removeIf { it.classifier == "unpacked" }
it.get().outgoing.artifact(minJar)
}
}
}
// for `publishToMavenLocal` task
afterEvaluate {
publishing {
publications {
withType<MavenPublication> {
if (name == "pluginMaven") {
setArtifacts(listOf(minJar))
}
}
}
}
}
Details
See https://github.com/ArcticLampyrid/gradle-git-version/commit/23ccfc8bdf16c9352024dc0b7722f534a310551f
I'm experimenting with gradle and trying to setup a system that builds different flavors (brands) of an application, which differ by configuration mainly. What I have so far are two versions of the build scripts - both not working.
Version 1
First flavor specific resource folder flavor-res is added to sourcesets, which achieves overwriting some default resources. A task rule defines tasks for each flavor, which should (ideally) trigger build of the whole jar.
This works fine and generates the required jar, for one flavor at a time, like
gradle clean flavorOne
but the shadowJar task runs only once, if I do
gradle clean flavorOne flavorTwo
Stripped down Script:
sourceSets {
main {
...
resources {
srcDirs = ['src/main/resources', "${project.buildDir}/flavor-res/"]
}
}
}
shadowJar { classifier = 'SNAPSHOT' }
tasks.addRule("Pattern: flavor<Name>") { String taskName ->
if (taskName.startsWith("flavor")) {
String flavorName = (taskName - "flavor")
String flavorOutDir = "${project.buildDir}/${flavorName}"
// Set output folder and jar name
task("${taskName}Configure") {
outputs.dir(flavorOutDir)
doFirst {
archivesBaseName = flavorName
project.buildDir = flavorOutDir
}
}
// Copy res to folder used in sourcesets
task("${taskName}CopyResources") {
mustRunAfter = ["${taskName}Configure"]
outputs.dir("${project.buildDir}/flavor-res")
doFirst {
copy {
from "flavors/${flavorName}/"
into "${project.buildDir}/flavor-res/"
}
}
}
shadowJar.mustRunAfter = ["${taskName}Configure", "${taskName}CopyResources"]
// Define task that depends on shadowJar
task(taskName, dependsOn: ["${taskName}Configure", "${taskName}CopyResources",
shadowJar]) {
println "Configuring ${taskName}"
}
}
Sensing that it probably doesnt work because the change detection somehow doesnt work, I tried an alternative approach. Here is a simplified version of script
Version 2
Modified the rule to define a shadowJar dynamic task for each flavor.
/* Removed sourceSets in this version */
shadowJar { classifier = 'SNAPSHOT' }
tasks.addRule("Pattern: flavor<Name>") { String taskName ->
if (taskName.startsWith("flavor")) {
String flavorName = (taskName - "flavor")
String flavorOutDir = "${project.buildDir}/${flavorName}"
// Set resources for main sourceset
task("${taskName}Configure") {
outputs.dir(flavorOutDir)
doFirst {
archivesBaseName = flavorName
sourceSets.main.resources.srcDirs = ['src/main/resources', "${flavorOutDir}/flavor-res"]
project.buildDir = flavorOutDir
}
}
task("${taskName}CopyResources") {
outputs.dir("${flavorOutDir}/flavor-res")
dependsOn "${taskName}Configure"
doFirst {
copy {
from "flavors/${flavorName}/"
into "${project.buildDir}/flavor-res/"
}
}
}
// This should shadowJar for each flavor - but generate jars dont have the required artifacts.
task ("${taskName}Build", type: ShadowJar) {
from sourceSets.main.output
configurations = [ configurations.runtime ]
classifier = 'SNAPSHOT'
dependsOn "${taskName}CopyResources"
}
task(taskName, dependsOn: ["${taskName}Build"]) {
}
}
}
However, now, the generated jars are malformed. The first flavor gets just the artifacts for main, but no showed jars. The second jar has just the manifest and nothing else.
What would be the correct way of achieving that.
PS: No, its not an android application (flavor is just a synonym for a brand).
I decided to recreate a flavor build script, because it can be simplified to what you have now. The ShadowJar task can handle copying all the classes and resources by itself, there is no need to define separate ones. I also took some default configuration that would have been applied to the shadowJar task and applied this to the custom ShadowJar tasks to get the same behavior.
I first build a quick test project structure which can be found here:
Test Structure
Then I came up with the following script:
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
id 'java'
id "com.github.johnrengelman.shadow" version "2.0.4"
}
group 'your-group'
version 'dev-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// Example dependency
compile group: 'com.google.guava', name: 'guava', version: '19.0'
}
tasks.addRule("Pattern: flavor<Name>") { def taskName ->
if (!taskName.startsWith("flavor")) {
return
}
def flavorName = taskName - "flavor"
// Define the shadow task
def shadowTask = task ("${flavorName}ShadowJar", type: ShadowJar) {
classifier = flavorName
// Add our flavor resources, first to prioritize these resources
from file("src/main/flavors/${flavorName}")
// Include our project classes
from project.sourceSets.main.output
// Don't include duplicate resources, only the first ones added, in
// this case the flavored resources will override the default ones
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// Some defaults taken from the default shadowJar task
// https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L48
configurations = [ project.configurations.runtime ]
manifest.inheritFrom project.tasks.jar.manifest
exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA')
}
// Define the flavor task
task ("${taskName}", dependsOn: shadowTask) {}
}
I'm using Gradle 4.4 (edit: issue still present in Gradle 4.8). For reasons my project has the following layout
src/main/java/com/company/common
src/main/java/com/company/mod1
src/main/java/com/company/mod2
The build can generate either mod1 or mod2, depending on the build task executed. Classes from mod1 shall never use classes from mod2 and vice versa. Therefore I want the build to fail, if either uses classes from the other. However I still want to be able to develop both sources in Eclipse, which is why I only want the build to fail on our CI server. The CI server provides a parameter CI_BUILD. The build file uses the following mechanism to allow this:
Excludes aren't applied properly here:
ext {
ext_template_mod1 = [:]
ext_template_mod1.src_excludes = "**/mod2/**"
ext_template_mod2 = [:]
ext_template_mod2.src_excludes = "**/mod1/**"
if (project.hasProperty("mod2")) {
ext_template = ext_template_mod2
} else {
ext_template = ext_template_mod1
}
}
sourceSets {
main {
java {
if (project.hasProperty("CI_BUILD")) {
exclude "${project.ext_template.src_excludes}"
}
}
}
}
For some reason this doesn't work. gradlew build -PCI_BUILD doesn't fail if a source file on mod1 references a source file from mod2.
I fail to understand why it doesn't. If I don't check for the project property, the exclude works as expected:
Working configuration:
ext {
ext_template_mod1 = [:]
ext_template_mod1.src_excludes = "**/mod2/**"
ext_template_mod2 = [:]
ext_template_mod2.src_excludes = "**/mod1/**"
if (project.hasProperty("mod2")) {
ext_template = ext_template_mod2
} else {
ext_template = ext_template_mod1
}
}
sourceSets {
main {
java {
exclude "${project.ext_template.src_excludes}"
}
}
}
Now gradlew build -PCI_BUILD fails as expected when a source file on mod1 references a source file from mod2.
But now my IDE won't recognize the sources in the mod2 folder as sources anymore.
How can I apply excludes to my source set based on the existence of a build parameter?
I've been creating a minimal example and it works just fine there:
apply plugin: 'java'
ext {
ext_template_mod1 = [:]
ext_template_mod1.src_excludes = "**/mod2/**"
ext_template_mod2 = [:]
ext_template_mod2.src_excludes = "**/mod1/**"
if (project.hasProperty("mod2")) {
ext_template = ext_template_mod2
} else {
ext_template = ext_template_mod1
}
}
sourceSets {
main {
java {
if (project.hasProperty("CI_BUILD")) {
exclude "${project.ext_template.src_excludes}"
}
}
}
}
jar {
if (project.hasProperty("CI_BUILD")) {
exclude "${project.ext_template.src_excludes}"
}
}
The issue above was an artifact of my own build. I had used dynamic task generation and forgotten to provide the parameter I was checking for as a startParameter.projectProperties.
I have a Spring Boot + Angular 2 project. I want to deploy it to Heroku.
I'm able to run the npm build then copy the generated files over to the public folder (src/resources/public) manually, then run the backend build.
What I want to do is to set up a gradle build that will do all of that at once.
What I have so far is a gradle build that will build the front end, build the backend, however it does not copy the static files before generating the jar. Since the jar does not contain said static files, it won't work on Heroku.
Here's the project folder structure:
root
backend
src/main/java
src/main/resources
frontend
--> angular files go here
build/libs -> where the JAR file goes
The gradle build file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
// spring
classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE')
classpath('org.springframework:springloaded:1.2.6.RELEASE')
}
}
plugins {
id "com.moowork.node" version "1.2.0"
}
// gradle wrapper
task wrapper(type: Wrapper) {
gradleVersion = '3.4'
}
// configure gradle-node-plugin
node {
version = '8.1.4'
npmVersion = '5.0.3'
download = true
workDir = file("${project.projectDir}/node")
nodeModulesDir = file("${project.projectDir}/")
}
// clean node/node_modules/dist
task npmClean(type: Delete) {
final def webDir = "${rootDir}/frontend"
delete "${webDir}/node"
delete "${webDir}/node_modules"
delete "${webDir}/dist"
delete "${webDir}/coverage"
delete "${rootDir}/backend/src/main/resources/public"
}
// clean task for npm
task copyFiles {
doLast {
copy {
from "${rootDir}/frontend/dist"
into "${rootDir}/backend/src/main/resources/public"
}
}
}
// build task for npm
task frontendBuild {}
frontendBuild.dependsOn(npm_install)
frontendBuild.dependsOn(npm_run_build)
npm_install {
args = ['--prefix', './frontend']
}
npm_run_build {
args = ['--prefix', './frontend']
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
sourceSets {
main {
java {
srcDirs = ['backend/src/main/java']
}
resources {
srcDirs = ['backend/src/main/resources']
}
}
}
copyFiles.dependsOn(frontendBuild);
compileJava.dependsOn(frontendBuild);
task backendBuild {}
backendBuild.dependsOn(compileJava)
backendBuild.dependsOn(jar)
jar.dependsOn(copyFiles)
repositories {
mavenCentral()
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers('org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8')
}
}
idea {
module {
inheritOutputDirs = false
outputDir = file("${buildDir}/classes/main/")
}
}
jar {
baseName = 'expense-splitter'
version = '0.0.1'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations {
dev
}
dependencies {
// spring
compile('org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE')
compile('org.springframework.boot:spring-boot-starter-data-jpa:1.5.2.RELEASE')
compile('org.springframework.boot:spring-boot-starter-security:1.5.2.RELEASE')
compile('org.apache.commons:commons-lang3:3.3.2')
// to make hibernate handle java 8 date and time types correctly
// it's marked as deprecated but we need to keep it until
// spring boot jpa starts using hibernate 5.2
compile('org.hibernate:hibernate-java8:5.1.0.Final')
// json web tokens
compile ('io.jsonwebtoken:jjwt:0.7.0')
compile 'mysql:mysql-connector-java'
// google gson
compile('com.google.code.gson:gson:2.8.0')
// jackson - parsing of java 8 date and time types
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.7')
// spring dev tools
dev('org.springframework.boot:spring-boot-devtools:1.5.2.RELEASE')
// testing
testCompile('org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE')
}
// run spring boot app
bootRun {
//addResources = true
classpath = sourceSets.main.runtimeClasspath + configurations.dev
jvmArgs = ["-Xdebug -agentlib:jdwp=transport=dt_socket,address=8080,server=y,suspend=n"]
}
// run all task
task runAll {}
runAll.dependsOn(bootRun)
Thanks in advance,
Try a different approach. Instead of manually copying the resources, tell Gradle that when it processes resources for the JAR, also take into consideration what is in frontend/dist/:
processResources {
from ('frontend/dist/') {
into 'public'
}
}
This should result in a JAR containing a public/ directory, with the contents of frontend/dist/ inside of it.
Gradle configuration for Spring Boot 1.5\2.x + Angular 2-6
Angular in sub-folder frontend
Frontend module
Crate build.gradle:
plugins {
id "com.moowork.node" version "1.2.0"
}
node {
version = '8.11.3'
npmVersion = '5.6.0'
download = true
workDir = file("${project.buildDir}/node")
nodeModulesDir = file("${project.projectDir}")
}
task build(type: NpmTask) {
args = ['run', 'build']
}
build.dependsOn(npm_install)
Note for Angular 6
Update outputPath value in angular.json to 'dist'
Backend module
Edit build.gradle for backend module:
Spring Boot 2.X:
bootJar {
archiveName = "yourapp.jar"
mainClassName = 'com.company.app.Application'
from('frontend/dist') {
into 'static'
}
}
Spring Boot 1.5.X:
jar {
archiveName = "yourapp.jar"
manifest {
attributes 'Main-Class': 'com.company.app.Application'
}
from('frontend/dist') {
into 'static'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Finally execute bootRepackage or bootJar task and check results in builds/libs
Assume that front end is located at the following folder: src/main/webapp/fe-ui/, the following solution for the Spring Boot version 2.1.1.RELEASE could be considered:
bootJar {
baseName = 'jar-name'
version = '0.1.0'
from('src/main/webapp/fe-ui/build') {
into 'public'
}
}
task installFeDependencies(type: NpmTask) {
args = ['install']
}
task buildFe(type: NpmTask) {
args = ['run', 'build']
dependsOn installFeDependencies
}
compileJava {
dependsOn buildFe
}
Running gradlew build will install, build front end as well as will invoke bootJar. The latter will package built front end bundle.
I'm trying to setup Gradle to run Android test using Roboelectric mocking framework.
I have an Eclipse workspace with this structure:
MyApp
src
gen
test
....
MyAppTest
libs
test (source folder linked to MyApp.test)
....
Tests runs fine in Eclipse manually configuring build path.
How can I configure Gradle build scripts in MyAppTest to run tests in MyApp project using Roboelectric?
I was able to get this working based on this solution
In summary, try adding the following to your build.gradle:
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
dependencies {
// Dependencies for your production code here.
compile 'some.library'
// localTest dependencies, including dependencies required by production code.
testLocalCompile 'some.library'
testLocalCompile 'junit:junit:4.11'
testLocalCompile 'com.google.android:android:4.1.1.4'
testLocalCompile 'org.robolectric:robolectric:2.2'
}
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
check.dependsOn localTest
Don't forget to alter your *Test.java to #RunWith(RobolectricGradleTestRunner.class):
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
#Override
protected AndroidManifest getAppManifest(Config config) {
String pwd = YourApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String root = pwd + "../../../src/main/";
return new AndroidManifest(
Fs.fileFromPath(root + "AndroidManifest.xml"),
Fs.fileFromPath(root + "res"),
Fs.fileFromPath(root + "assets"));
}
}
You will then be able to run your test via gradle compileDebugJava localTest. If I remember correctly, this will require a newer version of gradle (perhaps 1.8 or 1.9)