Why can't Gradle find existing artifact? - java

I posted the same question at the Gradle Forums Gradle can not find existing artifact?.
Gradle v4.10.2
This really bothers me, since I know it should work, and I feel like I’m setting up my build.gradle file correctly.
I built a plugin and deployed it to our AWS S3 Maven repo.
When I run a Gradle command, e.g., ./gradlew clean, I get
Here's my build.gradle file
buildscript {
repositories {
maven {
url s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
accessKey AWS_SECRET_KEY
}
}
}
dependencies {
classpath "com.zift.utilities:zift-version-plugin:0.0.1"
}
}
plugins {
id 'java'
id 'maven'
id 'maven-publish'
id 'java-gradle-plugin'
}
version = '0.0.1'
jar {
manifest {
attributes 'artifactId': project.artifactId,
'groupId': project.groupId,
'version': project.version
}
baseName artifactId
doLast {
println "artifactId: $project.artifactId\ngroupId: $project.groupId\nversion: $version"
}
}
If I comment out the classpath line (the problematic line), I get
$ ./gradlew build
> Task :jar
:jar: No valid plugin descriptors were found in META-INF/gradle-plugins
artifactId: zift-version-plugin
groupId: com.zift.utilities
version: 0.0.1
What am I missing?

Related

Gradle Builds an Empty Jar File

I converted a project to gradle using gradle init from maven after updating the dependencies. gradle test works as expected. However, when I run gradle build, the jar file that's generated is entirely empty.
I've attempted to tweak the source set to make something happen, but that doesn't seem to solve the problem. The directory structure matches what gradle expects from what I can tell everything is nested in src/main/groovy
The project's full code is available on Github.
In general what causes no files to be added to a build? Is there additional configuration I need to add besides whatever gradle init creates?
Gradle build file:
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
implementation 'com.github.javafaker:javafaker:1.0.2'
testImplementation 'org.spockframework:spock-core:2.0-M3-groovy-3.0'
testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
}
group = 'nl.topicus.overheid'
version = '0.2.0'
description = 'java-factory-bot'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
What going on now is the project is try to build as Java project and get src from src/main/java as it is default from Java project. So, you need id 'groovy' not id 'java' in plugins section to make it look into src/main/groovy and build both .java and .groovy files in there.

Gradle custom plugin jar with dependencies

I'm trying to build a jar for a custom gradle plugin to be used by other gradle projects. I'm using java to write the plugin. I'm having a problem including dependencies in my jar. If I build the jar using the below build.gradle
plugins {
id 'groovy'
}
repositories{
mavenCentral()
}
dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.google.guava:guava:27.0-jre'
testCompile 'junit:junit:4.12'
//compile 'org.apache.commons:commons-lang3:3.8.1'
}
group = 'com.mine'
version = '1.0'
I get a NoClassDefFound exception for guava classes when applying the plugin on a project. If I include a task to create a jar with dependencies like below in the build.gradle
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it)}
}
}
It says Plugin with Id 'my-plugin' not found. How do I include dependencies in a gradle plugin jar?
Your plugin project should be configured as a standalone Plugin project and then published to a maven repository, which will make dependencies resolution work; there is good documentation about writing custom plugin here, specially the following part : using Gradle plugin development plugin
There is also a good example of writing/publishing/consuming a custom Plugin in the Gradle examples here : https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/plugins (see the two subprojects publishing and consuming )
And here is a working example with a plugin that has dependency on external library (commons-lang for example):
Plugin project
build.gradle
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
}
group 'org.gradle.sample.plugin'
version '0.1'
// pugin metadata configuration
gradlePlugin {
plugins {
myplugin {
id = "org.gradle.sample.plugin.myplugin"
implementationClass = "org.gradle.sample.plugin.MyPlugin"
}
}
}
// publish to local maven repo for testing
publishing {
repositories {
maven {
url "../repos/maven-repo"
}
}
}
// repo for dependences resolution
repositories{
jcenter()
}
// dependencies of this plugin
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
}
Plugin implementation : src/main/groovy/org/gradle/sample/plugin/MyPLugin.groovy
package org.gradle.sample.plugin
import org.apache.commons.lang3.StringUtils
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyPlugin implements Plugin<Project> {
#Override
void apply(final Project project) {
println "Applying custom plugin... "
project.tasks.create('testPlugin'){
doLast{
println " custom plugin task executing."
println "Result: " + StringUtils.capitalize("stringtotest")
}
}
}
}
Build and publish this plugin ./gradlew publish : the plugin jar and "plugin marker artefacts" will be published to local maven repo in ../repos/maven-repo
Consumer project
build.gradle
plugins {
id 'java'
// import/apply your custom plugin
id 'org.gradle.sample.plugin.myplugin' version '0.1'
}
group 'org.gradle.sample.plugin'
version '0.1'
repositories{
maven {
url "../repos/maven-repo"
}
jcenter()
}
To test the plugin, try to execute the plugin task testPlugin
> Task :testPlugin
custom plugin task executing.
Result: Stringtotest
Sorry to add this as an answer but I don't have enough points to comment (yes it is a bit late in coming but I found this in a search and it came so close, maybe this will help someone else).
The answer by #M.Ricciuti is correct, just missing one file, namely a settings.gradle in the referencing project (not the plugin) directory:
pluginManagement {
repositories {
maven {
url '../repos/maven-repo'
}
gradlePluginPortal()
ivy {
url '../repos/ivy-repo'
}
}
}
Many thanks, I have tried many things that didn't work before finding this, even the examples by gradle didn't work (or more likely I didn't run them correctly). Anyway I merged what I saw in the answers with M. Ricciuti's answer and saw that file in the sample.
My complete project is at https://github.com/reddierocket/sampleGradlePlugin
The readme has instructions to run it. (Note I did not include the wrapper but I am using gradle version 5.3.1.)

Intellij and Eclipse unable to find library artifact

I've a library project in Android studio that will be used in both Android and Java projects. I'm publishing this library to my own remote artifact repository in both .aar and .jar formats using the maven-publish plugin that's shipped with Android Studio. I can fetch and use the library properly in an Android app however when I try to use it in a Java project, intellij and eclipse are unable to detect even if the library is there even though gradle/maven is fetching the library from the remote repo. I've tried pushing only aar to the remote repo and fetching that in Java projects but that didn't workout either. Please note that I've no Android dependency/resources in this library. Following is the script being used to generate and push the artifacts to remote server.
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = "${_groupId}-${_artifactId}-${android.defaultConfig.versionName}.aar"
}
}
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
}
task deleteOldJar(type:Delete){
delete 'release/MyLibrary.jar'
}
task exportJar(type:Copy){
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
rename('classes.jar','MyLibrary.jar')
}
publishing {
repositories {
maven {
url _remoteRepoUrl
}
}
publications {
library(MavenPublication) {
groupId _groupId
artifactId _artifactId
version android.defaultConfig.versionName
artifact(sourceJar)
artifact(exportJar)
artifact ("$buildDir/outputs/aar/${_groupId}-${_artifactId}-${android.defaultConfig.versionName}.aar") { //aar artifact you want to publish
builtBy assembleDebug
}
//generate pom nodes for dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each { dependency ->
println "dependency name is: "+dependency.name
if(!dependency.name.equals('unspecified')) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
dependencyNode.appendNode('type', "aar")
}
else
println "dependency name is unspecified"
}
}
}
}
}
What am I doing wrong?
I found the solution. Turns out the com.android.library plugin in gradle was somehow messing with the generated artifact such that only Android projects could use the library artifact and not the plain java projects. Since my library doesn't have any layout resources, I can skip making .aar and just generate a jar file. For generating the jar file and publishing it to the remote repo following snippet is being utilized:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
def _remoteRepoUrl = "http://myserver.com/repo"
def _groupId = group
def _versionName = version
repositories {
mavenCentral()
jcenter()
maven {
url _remoteRepoUrl
}
flatDir {
dirs 'libs'
}
}
jar {
from sourceSets.main.output
}
task jarWithSources(type: Jar) {
from sourceSets.main.output
if (gradle.startParameter.taskNames.any{it == "publishToMavenLocal"}) {
from sourceSets.main.allJava
}
}
// Define how maven-publish publishes to the nexus repository
publishing {
repositories {
maven {
url _remoteRepoUrl
}
}
publications {
mavenJava(MavenPublication) {
from components.java
}
// publish the <project> jar as a standalone artifact
mavenJar(MavenPublication) {
artifact jarWithSources
//from components.java -- can't have both the above line and this
//artifactId "${jar.baseName}_jar"
version project.version
}
}
}
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.7'
}
}

Running Flyway migrations on gradle refresh

I am still trying to wrap my head around gradle so please bear with me.
I'm trying to set up flyway migrations to run automatically on every gradle refresh/build instead of having to run a command-line command. So when someone is pulling the latest changes including some new migrations their local db will be up to date.
I have this build.grade in the root dir:
buildscript {
...
repositories {
mavenLocal()
mavenCentral()
}
allprojects {
...
}
project(":core") {
apply plugin: "java"
dependencies {
compile project(":shared")
}
}
project(":server") {
apply plugin: "java"
dependencies {
compile project(":shared")
}
}
project(":shared") {
apply plugin: "java"
}
tasks.eclipse.doLast {
delete ".project"
}
Where I've tried merging the lines from the flyway get started pages.
I basically want to run the $ gradle flywayMigrate -i command from within the build.gradle file. I've tried with build.finalizedBy(flywayMigrate) but to no avail.
How would I do something like that?
Thanks in advance!
In order to make build finalized by flywayMigrate you need to run gradle build which probably You didn't.
To run flywayMigrate you can use defaultTasks which will run the tasks configured if no other are provided. So:
buildscript {
dependencies {
classpath 'com.h2database:h2:1.4.191'
}
}
plugins {
id "org.flywaydb.flyway" version "4.1.2"
}
flyway {
url = 'jdbc:h2:file:./target/foobar'
user = 'sa'
}
defaultTasks 'flywayMigrate'
When it comes to configuring -i (logging level) from within build.gradle it seems to be impossible with the newest gradle versions.

Publishing locally with Gradle not working

I have the following directory structure:
myapp/
src/main/resources/
<lots of code>
build.gradle
With the following build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
group = 'net.myuser'
repositories {
jcenter()
}
dependencies {
compile(
<dependencies here>
)
}
jar {
baseName = 'myapp'
}
task writePom << {
pom {
project {
groupId group
artifactId 'myapp'
version version
inceptionYear '2015'
licenses {
license {
name 'myapp'
distribution 'Blah blah blah'
}
}
}
}.writeTo("build/libs/pom.xml")
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
artifacts {
archives(file("${buildDir}/libs/myapp-${version}.jar")) {
name "myapp"
classifier ""
}
}
When I do:
./gradlew clean build writePom install -Pversion=0.1.0
I get two problems:
Inside the generated build/libs/pom.xml the groupId is showing as null; and
I cannot find a net.myuser directory under ~/.gradle/caches/modules-2, which tells me install is not working
So I ask: What do I need to change so that groupId isn't null, and how do I get install publishing all of the following:
JAR
Sources JAR
Javadoc JAR
pom.xml
As per comment, use groupId project.group instead of groupId group in order to set the <groupId> properly.
Regarding the install task, please have a look at Gradle Maven Plugin documentation:
Installs the associated artifacts to the local Maven cache, including Maven metadata generation.
By default, the local Maven cache is located in ~/.m2/repository, thus you are looking at the wrong location. The install task does not tamper with ~/.gradle/caches/modules-2 which is (as the name already implies) only a cache for resolved dependencies.
By using the Maven plugin, Gradle already creates a POM file for you. So please check if you really need a custom writePom task.
Also, the main artifact is installed automatically, so this might be redundant:
artifacts {
archives(file("${buildDir}/libs/myapp-${version}.jar")) {
name "myapp"
classifier ""
}
}

Categories