Use gradle plugin in parent project's buildSrc - java

How can a subproject in a multiproject Gradle build use a Gradle plugin that's defined in the root project's buildSrc folder?
I've basically got:
settings.gradle
buildSrc/src/main/groovy/com/example/conventions/JavaConventionsPlugin.groovy
buildSrc/build.gradle
common/build.gradle
and I'm trying to apply the plugin in the common project:
apply plugin: 'com.example.conventions.JavaConventionsPlugin'
but it says
Plugin with id 'com.example.conventions.JavaConventionsPlugin' not
found.

Looks like the plugin layout is different in buildSrc and I should have been following this example: https://docs.gradle.org/current/samples/sample_convention_plugins.html
buildSrc/build.gradle
buildSrc/settings.gradle
buildSrc/src/main/groovy/example.java-conventions.gradle
buildSrc/src/main/groovy/com/example/conventions/JavaConventionsPlugin.groovy
and applying using:
plugins {
id 'example.java-conventions'
}

Related

What is the purpose of apply maven plugin in build.gradle?

I keep seeing apply plugin: 'maven' in build.gradle files for Java projects. What is the purpose of adding a Maven plugin? I can't seem to find the plugin when querying for it on plugins.gradle.org.
Why don't you just google it?
http://sorcersoft.org/project/site/gradle/userguide/maven_plugin.html
"The Maven plugin adds support for deploying artifacts to Maven repositories."

Apply gradle custom module plugin

I created a plugin for gradle with java.I want to test it.
so, I created a another gradle project add I added my plugin as a module.
Now , How can I apply my module as a plugin in build.gradle file ?
My project structure
My plugin
version '1.0.0'
id = 'com.hello.first.plugin'
implementationClass = 'com.hello.first.plugin'
Here I saw for applying jar as a plugin in gradle : ANSWER.
How can I add module as a plugin in build.gradle ?
You need to make it a standalone project and publish it, at least to mavenLocal().
Then just add mavenLocal() into buildscript.dependencies and it should resolve.
A local flatDir repository should work, too.

Gradle: find resolved version of a dependency imported with +

I want to print the last version of a dependency in gradle.
I added my dependency in this way :
compile 'test:test:+'
now I want to print the version of my dependency, because I want to know which version I'm using.
I'm using it in this way :
gradle dependencyInsight --configuration compile --dependency test:test
But my output is this :
+--- test:test:+ -> project : (*)
Is there anyway I can get the real version of my dependency and not the +?
Within app module's build.gradle I've imported Square's Moshi library as follows:
dependencies {
compile 'com.squareup.moshi:moshi:+'
}
Then I executed following command in terminal:
./gradlew app:dependencyInsight --configuration compile --dependency com.squareup.moshi:moshi
Here's the output that I've received:
All easy, open hierarchy of view Project and see External Libraries
If you want to check the overview for all your dependencies, you can check with this command -
Solution 1-
./gradlew app:dependencies
Or
Solution 2-
If you want to check for any specific dependency.you can use gradles' build-in 'dependencyInsight : -
gradle dependencyInsight --configuration compile --dependency compile 'test:test:+'
or
Solution 3-
You can check your project .idea folder
inside your project -> .idea/libraries
there also you can see the final version of dependencies used.
You can do the following:
Use the configuration that contains your jar file
Filter for the the jar file's name
Print the results
This will print the full path as well as the version. You can extract just the jar name if needed.
task printPmdVersion << {
FileTree pmdJar = zipTree(configurations.pmd.filter {
dep -> dep.name.contains("pmd-core")
}.singleFile)
println pmdJar
}
Example of output:
ZIP '/home/user/java/gradle_user_home/caches/modules-2/files-2.1/net.sourceforge.pmd/pmd-core/5.4.1/28715c2f768b58759bb5b373365997c30ac35899/pmd-core-5.4.1.jar'
Once you have added your dependency as "compile 'test:test:+'" build the project.
Then within the "Project" folder structure hierarchy find that dependency within "External Libraries" at the bottommost of folder structure , it will along with its version there. Use that version with your dependency and re-sync/build project again.
It's not a best practice use the '+' sign to always use the latest library version because you could not be able to have a repeatable build if you need one.
I mean, if you have to checkout your previous version of your APK from your Source Control Management system (e.g. Git) that you know it works fine, if you compile today (new library version could have been release)... maybe your old friend APK that was working fine... now it doesn't work fine like your latest one.
That said I suggest you using a gradle plugin like that:
https://github.com/ben-manes/gradle-versions-plugin
You will install in your build.gradle at project level like that:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'com.github.ben-manes.versions'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And you'll find a new gradle task named dependencyUpdate that if you lunch it it will report you all your library versions compared with the latest ones:
------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------
The following dependencies are using the latest milestone version:
- com.github.ben-manes:gradle-versions-plugin:0.17.0
- junit:junit:4.12
The following dependencies have later milestone versions:
- com.android.support:appcompat-v7 [26.1.0 -> 27.0.2]
- com.android.support.constraint:constraint-layout [1.0.2 -> 1.1.0-beta5]
- com.android.support.test.espresso:espresso-core [3.0.1 -> 3.0.2-alpha1]
- com.android.tools.build:gradle [3.0.1 -> 3.2.0-alpha03]
- org.jacoco:org.jacoco.agent [0.7.4.201502262128 -> 0.8.0]
- org.jacoco:org.jacoco.ant [0.7.4.201502262128 -> 0.8.0]
- com.android.support.test:runner [1.0.1 -> 1.0.2-alpha1]

Gradle Project convert to Maven

I have started to learn Gradle and i want to know how to convert a gradle project to maven project. I took a gradle project from the below link :
https://github.com/rjrudin/ml-camel-mlcp
I was able to generate a jar but a POM.xml is not generated for the above project. I want to stuff the jar file and test the functionality using maven. I took help from the below link to convert it to maven but it did not help :
https://codexplo.wordpress.com/2014/07/20/gradle-to-maven-conversion-and-vice-versa/
Please help me resolve this and thanks in advance.
If you are interested in generating a pom.xml for publishing your artifact, Gradle has the maven-publish plugin. You can use the MavenPublication class to produce (and customize) a pom.xml.
An example:
apply plugin: "java"
apply plugin: "maven-publish"
task sourceJar(type: Jar) {
from sourceSets.main.allJava
classifier "sources"
}
publishing {
publications {
myPublication(MavenPublication) {
from components.java
artifact sourceJar
pom.withXml {
asNode().appendNode('description', 'A demonstration of Maven POM customization')
}
}
}
}

Copy jar from dependent Project in Gradle?

I have to gradle projects core and main. They are in a flat project structure:
/
/core
/main
Both projects use the java plugin. The main project has a settings.gradpe file with the following content:
includeFlat "core"
The build.gradle of main is:
apply plugin: 'java'
apply plugin: 'eclipse'
...
dependencies {
compile (project(':core'))
}
When I execute the build task on the main project. The core project is build first and the generated core-1.0.jar is used as dependency for the compile task of main.
When I use the build task on main I want that the core-1.0.jar from the core project is copied into the lib folder of my main project.
How can I do that?

Categories