Gradle uploadArchives artifacts namespace when depending on a plugin - gradle

I have a Android app building with Gradle. I am using the gradle-release plugin to create automatic releases, and then the uploadArchives task from Gradle to upload the generated .apk to a Maven repo (Nexus).
I have to add the archives to upload dynamically at runtime, because my build is using custom Android flavors.
Everything works fine when I run the uploadArchives from the command line:
variant.outputs.each { output ->
def apkFile = output.outputFile
tasks."assemble${capitalizedVariantName}" << {
artifacts.archives [file: apkFile, classifier: variant.baseName]
}
}
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = PROJECT_GROUP
pom.artifactId = PROJECT_NAME
}
}
}
Then I run:
./gradlew assembleFlavorNameRelease uploadArchives
And the .apk is correctly uploaded to Nexus.
I have the need to run the uploadArchives task BEFORE the release plugin automatically changes the version name of the project and commit.
Basically:
- current version: 0.1.0-SNAPSHOT
- run release
- version becomes: 0.1.0
- build (build task)
- upload this build to Nexus (uploadArchives task)
- update the version to: 0.1.1-SNAPSHOT (updateVersion task)
To achieve this, what I have done is to have the updateVersion task of the gradle-release plugin depending on the uploadArchives
updateVersion.dependsOn uploadArchives
Well, when I do this, the artifacts.archives. is empty, so no upload.
I suspect that, maybe, since I add the uploadArchives task as dependency of a task of the release plugin, then the "namespace" is different, so basically the uploadArchives task does not use the "same instance" of artifacts.archives, filled during the build.

updateVersion.dependsOn uploadArchives
If you do that then you end up calling uploadArchives in the same process as the release is done, but not in the same process the build is done. To get the version right for the build task itself, the release plugin spawns a new gradle build which runs the build with the correct version number. (This is done because a lot of other plugins like maven-publish are not able to pickup on a changed project version during runtime)
If you want to execute tasks in the same process as the build you need to either use the tasks beforeReleaseBuild or afterReleaseBuild to depend on. Both of them are run in the same process.
So in your case it would be
afterReleaseBuild.dependsOn uploadArchives
This runs the uploadArchives directly after the build has finished with the release version.
For a better understanding I adapted your taskgraph:
- current version: 0.1.0-SNAPSHOT
- run release
- version becomes: 0.1.0 (and is written to gradle.properties)
- spawn new gradle build
- build (build task)
- upload this build to Nexus (uploadArchives task)
- update the version to: 0.1.1-SNAPSHOT (updateVersion task)

Related

How to run remote debug on local kotlinc

I want to compile some sample kotlin project using local compiler. I clone jetbrains/kotlin project from githib and build it. And now i have local compiler in /dist folder. How i need to configure gradle in sample project to use this local compiler in it and have an ability for remote debugging?
The simplest way is to build the whole toolchain and use it in your test projects.
To do that, run the install Gradle task in the Kotlin project:
./gradlew install
This will publish all of the project's Maven modules to the Maven local repository (~/.m2/repository by default) with the default snapshot version, which is 1.3-SNAPSHOT at the moment.
Then, in your test Gradle project:
If you apply the Gradle plugin using a buildscript block and an apply statement, add the mavenLocal() repository to buildscript { repositories { ... } } and use the snapshot version of the Gradle plugin:
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3-SNAPSHOT")
}
}
If the plugins are applied using the plugins { ... } block, modify the settings.gradle script and add the following:
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}
and again, use the 1.3-SNAPSHOT version of the Gradle plugin:
plugins {
id("org.jetbrains.kotlin.jvm").version("1.3-SNAPSHOT")
}
In order to be able to debug the compiler, you need to run its process with a debugging agent waiting for a connection. Normally, the compiler is run in a daemon process that is harder to connect to. It's much simpler to run the compiler inside the Gradle process. To do that, you need to set a system property kotlin.compiler.execution.strategy to in-process in the Gradle process (note: this should be not a Gradle project property but a Java system property which can be passed by -Dkey=value or set using System.setProperty(...).
Then run a Gradle build with a command line -Dorg.gradle.debug=true to make Gradle wait for a remote debugger. I would advise for running the test project build from the terminal, not the IDE.
This will look like:
./gradlew compileKotlin -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy=in-process
Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details
> Starting Daemon
(at this point, the build seems to hang, but it just waits for the debugger, so proceed below)
In the IDE where you work with the Kotlin project, put some breakpoints in the compiler code and attach the remote debugger to the Gradle process.
Note that, with the Kotlin compiler running inside the Gradle process, the latter may run out of memory sooner. Make sure the Gradle process gets enough heap space.

How can I automatically run google-java-format as part of my Gradle build?

Google-java-format-gradle-plugin integrates with Gradle, but how can I run it automatically as part of the normal build?
Sherter gradle plugin is automatically integrated to "gradle build". When you run it, it will run "gradle verifyGoogleJavaFormat". In case of violations, the build will fail.
We are using it on jenkins and it works. You will only need dependency to build.gradle file:
compile group: 'com.github.sherter.google-java-format', name: 'com.github.sherter.google-java-format.gradle.plugin', version: '0.8', ext: 'pom'
And also add plugin:
id 'com.github.sherter.google-java-format' version '0.8'
Then just run "gradle build" and you can see in the console, that verifyGoogleJavaFormat was executed.
DependsOn plugin's task that you need from a task that relates to a normal build, for example, you can use preBuild task:
tasks.findByName("preBuild").dependsOn(YOUR_TASK_FROM_PLUGIN)
or shorter
preBuild.dependsOn(YOUR_TASK_FROM_PLUGIN)
Also you can choose another task instead of preBuild.

How to skip testRuntime task in gradle?

In my eclipse, there are many projects that build with gradle. I want to skip the testRuntime task in gradle. Because It's too slow and makes me wait for long time whenever I change some code or give changes the projects. How can I avoid this? I want to apply every relevant projects.
UPDATE
This is the result of gradle task. Eclipse is executing background job and I check these jobs in the process tab and I can see this message like the followings.
Gradle Build on xxx project
Executing tasks : xxxx:TestRuntime
I think It takes time because of firewall. I want to skip this process. I guess, It can be done by eclipse or by gradle configuration.
My build.gradle file
buildscript {
repositories {
mavenLocal()
maven { // for thridparty lib
url 'http://70.121.244.11:8081/nexus/content/groups/openpms-public'
credentials {
username 'openpms'
password 'openpms0'
}
}
maven { // for thridparty lib
url 'http://70.121.244.11:8081/nexus/content/repositories/thirdparty'
credentials {
username 'developer'
password 'developer0'
}
}
maven { url 'http://70.121.224.52:8081/nexus/content/repositories/central/' }
jcenter{ url 'http://70.121.224.52:8081/nexus/content/repositories/jcenter/' }
}
dependencies {
classpath "org.akhikhl.gretty:gretty:$gretty_version"
}
}
repositories {
mavenLocal()
maven {
url 'http://70.121.244.11:8081/nexus/content/groups/openpms-public'
credentials {
username "$maven_username"
password "$maven_password"
}
}
maven { // for thridparty lib
url 'http://70.121.244.11:8081/nexus/content/repositories/thirdparty'
credentials {
username 'developer'
password 'developer0'
}
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'
sourceCompatibility = 1.6
targetCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' // compileGroovy, compileTestGroovy
def spring_version = '4.0.6.RELEASE'
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'org.opensymphony.quartz', name: 'quartz', version: '1.6.1'
compile group: 'commons-logging', name: 'commons-logging', version: '1.0.4'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}
gretty {
servletContainer = 'jetty8'
port = 8080
contextPath = '/agent.server'
}
UPDATE 2
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] tasks
[sts] -----------------------------------------------------
<org.gradle.api.plugins.quality.PmdExtension_Decorated#729eaf4b __dyn_obj__=org.gradle.api.internal.AsmBackedClassGenerator$MixInExtensibleDynamicObject#2fc49256 __mapping__=org.gradle.api.internal.ConventionAwareHelper#1263649a __consoleOutput__=false __ignoreFailures__=true __reportsDir__=true __ruleSetConfig__=false __ruleSetFiles__=true __ruleSets__=true __sourceSets__=true __targetJdk__=false __toolVersion__=true project=root project 'redca-agent' ruleSets=[basic] targetJdk=null ruleSetConfig=null ruleSetFiles=file collection consoleOutput=false toolVersion=4.3 sourceSets=[source set 'main'] ignoreFailures=true reportsDir=D:\workspace\redCAGit\redca-agent\build\reports\pmd>
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Application tasks
-----------------
bootRun - Run the project with support for auto-detecting main class and reloading static resources
installApp - Installs the project as a JVM application along with libs and OS specific scripts.
run - Runs this project as a JVM application
Build tasks
-----------
assemble - Assembles the outputs of this project.
bootRepackage - Repackage existing JAR and WAR archives so that they can be executed from the command line using 'java -jar'
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes 'test'.
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
components - Displays the components produced by root project 'redca-agent'. [incubating]
dependencies - Displays all dependencies declared in root project 'redca-agent'.
dependencyInsight - Displays the insight into a specific dependency in root project 'redca-agent'.
help - Displays a help message.
model - Displays the configuration model of root project 'redca-agent'. [incubating]
projects - Displays the sub-projects of root project 'redca-agent'.
properties - Displays the properties of root project 'redca-agent'.
tasks - Displays the tasks runnable from root project 'redca-agent' (some of the displayed tasks may belong to subprojects).
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Other tasks
-----------
findbugsTest - Run FindBugs analysis for test classes
pmdTest - Run PMD analysis for test classes
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
Total time: 2.354 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 2 sec
[sts] -----------------------------------------------------
I want to know the way to skip working testRuntime.
Even if it is not task, my concern is not to waste time.
How can I avoid this?
I am a newbie in gradle and I am not goot at English. Please understand.
Anyway, I solved by myself. Eclipse gradle plugin, I am using, is trying to connect the repository on the internet that may be blocked by firewall. So I can't solve this. There are many workplaces including the place where is offline.
I could skip this giving the parameter to Program Arguments like the followings. you can add this property in eclipse preference > Gradle > Arguments.
--offline

Debug Gradle plugins with IntelliJ

Problem
I want to use the interactive debugger with IntelliJ. Unfortunately, I can't convince IntelliJ to load and compile the plugin. However, I can do gradle clean build and the plugin builds and runs its tests as expected.
Specifically, I'm trying to debug local changes to gradle-js-plugin and IntelliJ says it can't find com.google.javascript.jscomp.CompilerOptions as well as spock.lang.Specification. (I'm thinking maybe it's something about the way they are loaded, but that's a guess.)
Things I've tried
NOTE: I didn't revert any processes between steps.
0. My First Guess
I noticed a howto on docs.codehaus.org. IntelliJ couldn't find org.gradle.launcher.GradleMain, so I've adapted it to use GradleLauncher with the following:
import org.gradle.GradleLauncher
class GradleScriptRunner {
public static void main(String[] args) {
GradleLauncher.newInstance(
"-p",
"/path/to/gradle-js-plugin/src/test/resources/build.gradle",
"clean assemble"
)
}
}
Per GradleLauncher's documentation.
Outcome: IntelliJ won't compile the project.
1. Per Peter Niederwieser's answer Fix idea project & debug via plugin
Steps
~# cd /path/to/gradle-js-plugin && gradle cleanIdea idea
Opened the newly created project and attempted to debug using the ScriptRunner from step 0.
Outcome: Project compiles (yay!), but I can only hit breakpoints in GradleScriptRunner.groovy.
2. Per Peter Niederwieser's answer run gradle CLI w/ special options
1 & 2. Merged for clarity:
~# export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
~# gradle clean assemble
Listening for transport dt_socket at address: 5005
Configure IntelliJ to connect to this port and start debugging (see image):
For this step I tried the following .gradle file configurations:
1. Use only build.gradle
--build.gradle--
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'js'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile findProject "/path/to/gradle-js-plugin"
}
}
repositories {
mavenLocal()
mavenCentral()
}
Outcome:
FAILURE: Build failed with an exception.
* Where:
Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 13
* What went wrong:
A problem occurred evaluating root project 'resources'.
> No such property: findProject for class: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 8 mins 50.498 secs
2. Use both build.gradle and settings.gradle
--settings.gradle--
include "/path/to/gradle-js-plugin"
--build.gradle--
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'js'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}
repositories {
mavenLocal()
mavenCentral()
}
Outcome:
FAILURE: Build failed with an exception.
* Where:
Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 5
* What went wrong:
A problem occurred evaluating root project 'resources'.
> Plugin with id 'js' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 13.553 secs
My Setup
Gradle
~# gradle -v
------------------------------------------------------------
Gradle 1.0
------------------------------------------------------------
Gradle build time: Tuesday, June 12, 2012 12:56:21 AM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.7.0_04 (Oracle Corporation 23.0-b21)
OS: Linux 3.2.0-2-amd64 amd64
Java
~# java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
IntelliJ
IntelliJ IDEA Ultimate 117.499 w/ Bundled Gradle plugin
Hoping for
Any tips that'll get me into debug mode within the plugin.
I was able to debug gradle sources (including plugins) using -Dorg.gradle.debug=true (found on gradle forum):
Stop daemons if any:
./gradlew --stop
Run
./gradlew <task> --no-daemon -Dorg.gradle.debug=true
Connect remotely to gradle process (port 5005) - if using IntelliJ IDEA, see OP's image above
It should stop on breakpoints now.
BTW, I have created a separate IntelliJ IDEA project for gradle sources. Since I use gradle wrapper, I have grabbed the sources from
~/.gradle/wrapper/dists/gradle-1.11-all/7qd8qq8te5j4f5q9aaei3gh3lj/gradle-1.11/src
In IDEA I did File->Import Project, then selected the above path, then - "Create project from existing sources". Hit Next couple of times (made sure I didn't include any jars from lib/plugins directory, since IDEA would complain that most project files already exist).
I then created a remote debug configuration in that IDEA project and used it for debugging gradle.
First, it sounds like there is a problem with your IDEA Gradle project. If you run gradlew cleanIdea idea and then open the generated project from IDEA (rather than using the JetGradle plugin), all should be fine.
Second, if you still can't get the GradleMain/GradleLauncher (the former class does exist) approach to work, another approach is to debug the Gradle build as an external application. For that you need to add -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 to the GRADLE_OPTS environment variable, run the build from the command line, wait until it suspends, and then start a "Remote" run configuration (with corresponding settings) from IDEA. At that point the debugger should connect to the Gradle process and you should be up and running.
IntelliJ IDEA 12.1 provides ability to debug gradle tasks out of the box - right-click target task at the JetGradle tool window tasks list and choose 'debug'

Gradle Plugin not found when using wrapper

I have a small gradle build which is using a 3rd party plugin it works great while running gradle commands I then added the wrapper task so I could distribute the code and it could be built with non gradle users. When I went to test the gradlew command I can not even run gradlew tasks it fails saying the plugin is missing.
Is there some other configuration that needs to happen?
My wrapper task:
task wrapper(type: Wrapper) {
gradleVersion = '1.0-milestone-2'
jarFile = 'wrapper/wrapper.jar'
}
Full build file: https://github.com/beckje01/Multi-Combobox/blob/master/build.gradle
Based on the documentation of this plugin you got to build it from the source code and put it into the directory lib/plugins of your Gradle distribution. My guess is that's what you did before you switched to the Gradle wrapper. Whenever you use the Gradle wrapper your locally installed distribution is not used anymore. Gradle downloads the distribution and puts it under ~/.gradle/wrapper/dists/gradle-1.0-milestone-2. One way to get this running would be to put the plugin in there as you did before. However, this won't make it running for somebody else that checks out your code and runs the build. This is the whole point of using the Gradle wrapper.
What I would do in your case is to upload the plugin artifact to a central repository and refer to it in your build script. It doesn't look like it would be available on Maven Central though. You can upload it to your GitHub project and refer to it in your build script like this:
buildscript {
repositories {
add(new org.apache.ivy.plugins.resolver.URLResolver()) {
name = 'GitHub'
addArtifactPattern 'http://cloud.github.com/downloads/[organisation]/[module]/[module]-[revision].[ext]'
}
mavenCentral()
}
dependencies {
classpath 'beckje01:jslib:0.5'
classpath 'com.google.javascript:closure-compiler:r706'
}
}

Resources