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 ""
}
}
Related
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.
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?
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'
}
}
In my build.gradle, I added the plugin:
apply plugin: 'maven'
Then using gradle install I can copy the resulted jar into the maven repository : ~/.m2/repository
However, my Gradle repository resides in ~/.gradle/caches/modules-2/files-2.1. How can I install the jar into this repository?
What worked for me is gradle install -Dmaven.repo.local=the/path/of/the/folder.
I don't know which IDE you are using but in eclipse you can add a new Run Configuration, in Gradle taks add install and in program arguments -Dmaven.repo.local=the/path/of/the/folder.
If you insist on manipulating the cache, then your best bet is to write a shell script that will manually replace latest JAR in the cache.
The reason is that Gradle does not come with this functionality built-in, as Gradle uses notion of "local cache" in a strict sense, as opposed to "local repository" which is used by Maven.
The difference is that you are never supposed to save files to local cache manually.
To solve your problem the recommended way: Suppose that project A is a dependency of project B. Then you can call publishToMavenLocal command in project A to refresh the depedency. Add mavenLocal() repository in gradle.build of project B, so every time you build project B, Gradle will check the local repository when resolving the dependency A.
mavenLocal() is resolved like this:
In order to make use of a non-standard local maven repository, you can use the following configuration in your build.gradle:
repositories {
maven {
url '/Users/manuelj/apache/maven/repository'
}
}
A build.gradle sample to create a Jar file along with its logback dependencies. using mavenlocale()
apply plugin: 'java'
apply plugin: 'eclipse'
version = '1.0'
sourceCompatibility = 1.7
target Compatibility = 1.7
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.mkyong.DateUtils'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it :
zipTree(it)
}
}
with jar
}
//Get dependencies from Maven central repository
repositories {
mavenCentral()
}
//Project dependencies
dependencies {
compile 'ch.qos.logback:logback-classic:1.1.2'
}
Reference create a Jar file along with its logback dependencies.
I am trying to build a Spring cloud application that provides details of a Customer and the order history he/she has made in the past via gradle. When I am executing the build and clean task, I am unable to obtain the target jar file because of this error.
Execution failed for task ':compileJava'.
This is the message shown in the ExceptionStackTrace of the error log.
org.gradle.tooling.BuildException: Could not execute build using Gradle installation 'C:\gradle\gradle-3.4.1'.
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:51)
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:29)
at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:41)
at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:57)
Below is my build.gradle file.
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'yBayApplication'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-turbine:1.2.3.RELEASE')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-hystrix-dashboard')
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-feign')
compile('org.springframework.cloud:spring-cloud-starter-eureka:1.2.3.RELEASE')
compile('org.springframework.cloud:spring-cloud-starter-ribbon')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.BUILD-SNAPSHOT"
}
}
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'
}
}
I have checked the Java version which gradle uses and it points to the right directory C:\Program Files (x86)\Java\jdk1.8.0_121 and I have also included the gradle.properties file in the project to be double sure that it fetches only the Java_home properties
gradle.properties
org.gradle.java.home=C:\Program Files (x86)\Java\jdk1.8.0_121
I had also changed the eclipse.ini file by adding the following lines.
-vm
C:\Program Files (x86)\Java\jdk1.8.0_121\bin\javaw.exe
Still I find that the gradle build is failing.
What am I doing wrong?