How to prioritize mavenLocal over artifactory repo in gradle? - gradle

I have multi-modules project and I'm using artifactory for resolving custom libraries:
build.gradle of parent project:
...
subporjects {
...
apply plugin: "com.jfrog.artifactory"
artifactory {
resolve {
contextUrl = ext.getProperty('ARTIFACTORY_URL')
repoKey = ext.getProperty('ARTIFACTORY_REPO_NAME')
username = ext.getProperty('ARTIFACTORY_USERNAME')
password = ext.getProperty('ARTIFACTORY_PASSWORD')
}
}
}
It works as expected my library is published to artifactory with gradle artifactoryPublish and then it's fetched from there. But in some cases I want to fetch my custom library from mavenLocal() repo. I have next subproject build.gradle
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'my-custom-library'
}
But as I can see it is still resolves from artifactory. Can I somehow prioritize mavenLocal() over it ?

The repository priority will be the order in which they were added to the RepositoryHandler
I'm guessing that the artifactory repository is added when the plugin is applied so you could delay this by
afterEvaluate {
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
}
Or maybe
evaluationDependsOnChildren()
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}

If all you want to do is to depend on one subproject from within another, you should declare dependencies using the project notation:
dependencies {
compile project(':shared')
}
https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies

Related

Can't Publish SubProjects with Gradle

I have a gradle project, it has jvm (Scala) subprojects, and I want to publish all of their jars to a m2 repository.
I applied maven-publish like this ...
allprojects {
apply plugin: 'maven-publish'
publishing {
repositories {
// path to somewhere in my repo
maven { url new File(project.rootProject.getProjectDir().absoluteFile.parentFile, ".m2-repo") }
}
}
}
... but it only publishes the root project. I've tried running publish from the subprojects; I get an up-to-date message but nothing is added to the repo on disk.
I'm using the 4.0.1 GradeWrapper
It needs to be;
apply plugin: 'maven-publish'
publishing {
repositories {
// publish to a "local" repo that I can also consume or upload as I wish
maven { url new File(project.rootProject.getProjectDir().absoluteFile.parentFile, ".m2-repo") }
}
// really, really publish things
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}

Publish Java artifact to Maven Local with Gradle

I am facing a problem when trying to install a generated jar into my local Maven Repository. The message error just show me 'task 'publish' is not found'
I am using this Gradle Script:
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'
jar {
baseName = 'mongofoundry'
version = '1.0.0'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
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'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Do you have some idea Why I am reading that error message?
Thanks.
UPDATED
Running the command as #RaGe mentioned, solved the problem:
gradle publishToMavenLocal
The correct task to publish artifacts to local maven is
gradle publishToMavenLocal
Check Maven locally
For developing and testing it is useful to check library locally
gradle settings for apply plugin: 'com.android.library' not apply plugin: 'java-library'(where you can use it by default)
apply plugin: 'maven-publish'
//simple settings
project.afterEvaluate {
publishing {
publications {
library(MavenPublication) {
//setGroupId groupId
setGroupId "com.company"
//setArtifactId artifactId
setArtifactId "HelloWorld"
version "1.1"
artifact bundleDebugAar
/* add a dependency into generated .pom file
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', 'com.company')
dependencyNode.appendNode('artifactId', 'HelloWorld-core')
dependencyNode.appendNode('version', '1.1')
}
*/
}
}
}
}
to run it using command line or find this command in Gradle tab
./gradlew publishToMavenLocal
Location
artefact will be added into .m2 folder
//Unix
~/.m2
//Windows
C:\Users\<username>\.m2
//For example
/Users/alex/.m2/repository/<library_path>/<version>/<name>.<extension>
build folder
<project_path>/build/outputs/<extension>
other repositories location
~/.gradle/caches/modules-2/files-2.1/<group_id>/<artifact_id>/<version>/<id>
//For example
/Users/alex/.gradle/caches/modules-2/files-2.1/com.company/HelloWorld/1.1/c84ac8bc425dcae087c8abbc9ecdc27fafbb664a
To use it add mavenLocal(). It is important to place it as a first item for prioritise it, which is useful for internal dependencies
buildscript {
repositories {
mavenLocal()
}
allprojects {
repositories {
mavenLocal()
}
}
and
dependencies {
implementation 'com.company:HelloWorld:+'
}
*Also remember if you use a kind of shared.gradle file (via apply from) you should set path which is relevant to project.gradle (not shared.gradle)
[iOS CocoaPod local]
This is how I did it with Kotlin DSL (build.gradle.kts) for my Android library:
plugins {
id("maven-publish")
// OR simply
// `maven-publish`
// ...
}
publishing {
repositories {
// Local repository which we can first publish in it to check artifacts
maven {
name = "LocalTestRepo"
url = uri("file://${buildDir}/local-repository")
}
}
publications {
// ...
}
}
You can create all the publications with the following command:
./gradlew publishAllPublicationsToLocalTestRepoRepository
Or just a single publication with this command:
./gradlew publishReleasePublicationToLocalTestRepoRepository
See Gradle documentations: Maven Publish Plugin for more information.
Add maven plugin to your project and then:
gradle clean install
Here is an alternative skeleton for Gradle 7.5.1 with Java 17
build.gradle
plugins {
id 'org.gradle.java'
id 'org.gradle.maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
withJavadocJar()
withSourcesJar()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'your-group'
artifactId = 'your-artifact'
version = "0.0.1"
from components.java
}
}
repositories {
mavenLocal()
}
}
Publishing
You can see more details on the publishing steps with --info
./gradlew --info publishToMavenLocal
Output Directory
Linux/macOS
/Users/<username>/.m2/repository/your-group/your-artifact/0.0.1
Windows
C:\Users\<username>\.m2\repository\your-group\your-artifact\0.0.1

Generate local maven repository with all dependencies using Gradle

What I currently have
dependencies {
compile ...
}
task copyDependencies(type:Copy) {
from configurations.compile
into 'build/dependencies/'
}
This task copies all the required dependencies to the build/dependencies/ directory. Inside the directory, it looks as follows:
/dep1-1.0.jar
/dep2-1.0.aar
...
So essentially what is known as flatDir in gradle terms.
What I'd like to have
It's a local maven repository with all these dependencies, instead of a flatDir.
If I understand your question correctly, you just want to publish to a local directory as if it were mavenLocal() but in a location you specify.
In that case, I believe you just need:
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "/path/to/wherever"
}
}
}
See the Maven Publish plugin docs

Gradle Multiple Maven Repo

I added a dependency to my build.gradle: spring-data-neo4j
It requires neo4j-cypher-dsl-2.0.1.jar/pom, which is only located in the repo: https://repo1.maven.org/maven2/.
However as per the output below gradle never looks at this repo for the artifact. How can I get gradle to search this repo as well.
//build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url "https://repo1.maven.org/maven2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
}
}
gradle build:
FAILURE: Build failed with an exception.
What went wrong: Could not resolve all dependencies for configuration ':compile'.
Could not find org.neo4j:neo4j-cypher-dsl:2.0.1. Searched in the following locations:
https://repo1.maven.org/maven2/org/neo4j/neo4j-cypher-dsl/2.0.1/neo4j-cypher-dsl-2.0.1.pom
https://repo1.maven.org/maven2/org/neo4j/neo4j-cypher-dsl/2.0.1/neo4j-cypher-dsl-2.0.1.jar Required by:
**:feedserver:1.0.0 > org.springframework.data:spring-data-neo4j:3.2.2.RELEASE**
* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Edit:---------------------------------------
Sorry I accidently posted the incorrect build.gradle contents above, which has repeated maven central locations. This is my actual build.gradle file...When I build using these settings I still get the error above:
buildscript {
repositories {
mavenCentral()
maven {
url "http://m2.neo4j.org/content/repositories/releases/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
}
}
neo4j-cypher-dsl is not in Maven central. It is available in Neo4j repository you have to add another repo something like this:
repositories {
maven {
url "http://m2.neo4j.org/content/repositories/releases/"
}
}
NOTE is not necessary to use another maven ponting to maven central using mavenCentral() is enough
EDIT 1
repositories section in buildscript just works for dependencies inside. In this case for spring-boot-gradle-plugin
buildscript {
ext {
springBootVersion = '1.1.9.RELEASE'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
In your case you want to add another dependencies for your project. So you need to add another repositories section out of buildscript
repositories {
mavenLocal()
mavenCentral()
maven {
url "http://m2.neo4j.org/content/repositories/releases/"
}
}

Publish (rootproject) pom without (rootproject) publishing artifact / packaging = pom

I'm migrating one of our projects from maven to gradle: it's a gradle multi-project & all subprojects are publishing artifacts to artifactory. So far so good.
The legacy (maven-based) build environment however also expects the root project to publish a pom file with the "packaging" node equal to "pom" (common maven behaviour, so it seems)
So now, I'm trying to have this generated by Gradle, but only find ways to customize an automatically generated pom for each artifact, I can't find a way to generate/upload a pom without publishing an actual artifact.
Workaround for now is to have the root project use the java plugin, generate/install an empty jar and manipulate the generated pom to conform to maven expectations (packaging=pom), but that's a hack.
Is there a way to have this root pom file generated with gradle ?
Example project:
settings.gradle
rootProject.name = 'MultiProject'
include 'child01', 'child02'
rootProject.children.each { it.name = rootProject.name + "-" + it.name }
build.gradle
subprojects {
apply plugin: 'java'
}
allprojects {
apply plugin: 'maven'
group = 'my_group'
version = '0.0.1-SNAPSHOT'
}
EDIT (current workaround), addition to build.gradle
// workaround to generate pom
apply plugin: 'java'
configurations {
pomCreation
}
task createPom {
ext.newPomFile = "${buildDir}/blabla.pom"
doLast {
pom {
project {
packaging 'pom'
}
}.writeTo(newPomFile)
}
}
install.dependsOn(createPom)
artifacts {
pomCreation file(createPom.newPomFile)
}
I would use the gradle maven-publish plugin for that. With that plugin you can define your specific pom and don't have to upload other artifacts. Here an example:
publishing {
publications {
maven(MavenPublication) {
pom.withXml{
def xml = asNode()
xml.children().last() + {
delegate.dependencies {
delegate.dependency {
delegate.groupId 'org.springframework'
delegate.artifactId 'spring-context'
delegate.version( '3.2.8.RELEASE' )
}
}
}
}
}
}

Resources