When using gradle with maven-publish, I get Cannot find wagon which supports the requested protocol: scp - maven

The error I get - ""
The property uploadURL is defined as scp://user#host/data/apps/repo/m2
Here are the relevant sections
configurations {
deployerJars
}
// Apply the java plugin to add support for Java
apply plugin: 'java'
//----------------------
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.researchgate:gradle-release:2.0.2'
}
}
//----------------------
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
//uploadURL is defined in a properties file. It is properly recognied
//by gradle
repository(url:upLoadURL)
uniqueVersion = false
}
}
}
//----------------------
apply plugin: 'net.researchgate.release'
//----------------------
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'maven central' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
compile 'org.springframework:spring-context:4.1.6.RELEASE'
compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.11'
}

You created a configuration called deployJars that is unnecessary.
You are missing the wagon dependency for the archives configuration:
dependencies {
archives "org.apache.maven.wagon:wagon-ssh-external:3.4.0"
}
So change deployJars to archives.
Then for uploadArchives, you need to specify the configuration:
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.archives // <-- missing
repository(url: "scpexe://yourhost/yourdir/")
uniqueVersion = false
}
}
}

Related

Could not find method mavenContent() for arguments

I am trying to add a private nexus maven repo/utility to a gradle project (referred: https://docs.gradle.org/current/userguide/declaring_repositories.html#maven_repository_filtering).
Posting this issue as I haven't found any solution yet.
// Apply the java-library plugin to add support for Java Library
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'spring-boot'
apply plugin: 'net.researchgate.release'
apply plugin: 'maven'
// Configure project-level settings.
project.ext {
mainClass = "${mainClass}"
configFile = "$rootProject.projectDir/src/resources/application.properties"
finalJarName = "${name}-${version}.jar"
javaVersion = '1.8'
springBootVersion = '1.3.4'
springBootActuatorClient = '1.5.3.RELEASE'
apacheCommonIOVersion = '2.5'
camelVersion = '2.17.2'
solrVersion = '5.4.0'
}
// JAR/FatJAR configuration
mainClassName = "${mainClass}"
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
flatDir {
dirs 'lib'
}
maven {
url "http://nexus.repo.com/nexus/content/repositories/releases/"
credentials {
username = "${mavenUser}"
password = "${mavenPassword}"
}
mavenContent {
releasesOnly()
}
}
}
// Configure the build script.
buildscript {
ext {
springBootVersion = '1.3.6.RELEASE'
systemrulesVersion = '1.16.1'
}
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'net.researchgate:gradle-release:2.6.0'
}
}
dependencies {
........
compile group: 'com.some.utility.third-party-integrations', name: 'third-party-integrations', version: '1.0.29'
}
Firstly, the above implementation gives me error as,
Could not find method mavenContent() for arguments [build_6vwlfgalblecd5o2a6rgk023i$_run_closure1$_closure3$_closure6$_closure8#36b310aa] on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated#10c2064a.
Secondly, if I remove that it generates an error for the dependency that I have added as below,
Could not resolve com.some.utility.third-party-integrations:third-party-integrations:1.0.29.
> inconsistent module metadata found. Descriptor: com.some.utility.third-party-integrations:third-party-integrations:1.0.0-SNAPSHOT Errors: bad version: expected='1.0.29' found='1.0.0-SNAPSHOT'

Generate a shadow jar by feature, with Gradle in a Java project

I try to use the java plugin feature (https://docs.gradle.org/5.3-rc-1/userguide/feature_variants.html) to declare 2 versions of the same dependency, and generate at the end, 2 jars:
java {
registerFeature('v1') {
usingSourceSet(sourceSets.main)
}
registerFeature('v2') {
usingSourceSet(sourceSets.main)
}
}
dependencies {
compileOnly project(':djobi-core')
v1Implementation(group: 'org.elasticsearch', name: 'elasticsearch-spark-13_' + scalaVersion, version:'6.2.2') {
exclude group: "org.scala-lang"
}
v2Implementation(group: 'org.elasticsearch', name: 'elasticsearch-spark-13_' + scalaVersion, version:'6.3.2') {
exclude group: "org.scala-lang"
}
}
ShadowJar {
}
But it generates only 1, is it a good way to use feature feature like this?
The default task shadowJar uses the runtime configuration, see the docs-
In order to shadow configurations v1 and v2 we can define two new tasks, of type ShadowJar (they need to be configured).
Actually, v1 and v2 could be defined as "normal" configurations, that is, avoiding to use the feature-variants (it is simpler; moreover when trying to use shadowJar and the v1Implementation above, we have an error (Resolving configuration 'v1Implementation' directly is not allowed).
See the edited example below; it can be built with gradle shadowJar1 shadowJar2.
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:5.0.0"
}
}
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'java'
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
configurations {
v1 {
extendsFrom(implementation)
}
v2 {
extendsFrom(implementation)
}
}
dependencies {
// tweaking deps here
v1('ant:ant:1.6')
v2('junit:junit:4.12')
}
task shadowJar1(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar ) {
classifier = 'v1'
configurations=[project.configurations.v1]
}
task shadowJar2(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar ) {
classifier = 'v2'
configurations=[project.configurations.v2]
}

Gradle doesn't use repositories from "allprojects"

If I comment "repositories" on my buildscript, I get an error - even though the repositories are already declared on my "allprojects".
allprojects {
//...
buildscript {
repositories {
maven {
url "http://www.exemple.com/repositories"
}
}
}
}
}
buildscript {
// repositories {
// maven {
// url "http://www.exemple.com/repositories"
// }
// }
dependencies {
classpath group: 'com.exemple', name: 'exemple', version: '1.2.3'
}
}
Why does gradle not use the repositories defined on allprojects ? The error that I get:
> Could not resolve all dependencies for configuration ':classpath'.
> Cannot resolve external dependency com.exemple:exemple:1.2.3 because no repositories are defined.
buildscript block refers to the classpath for the current script, not a project. You can use it only for a Gradle script. For example:
example.gradle
buildscript {
repositories {
maven {
url "http://www.example.com/repositories"
}
}
dependencies {
classpath group: 'com.example', name: 'example', version: '1.2.3'
}
}
}
}
// do something, add tasks, etc.
build.gradle
subprojects {
apply from: 'example.gradle'
}

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

Upload only war/jar files in gradle(restrict zip/tar generation and upload)

My build script is like as follows. I use gradle build command to build and gradle upload command to upload the artifact. My problem is a tar,zip file is also generated with this command and get uploaded. I dont want it. Only things I would like to get uploaded is 'jar' and 'war' files.
I have also a related question posted by me yesterday here.
More details(I have excluded some unwanted code)
build file in root
allprojects {
apply plugin: 'maven'
group = 'groupid'
version = '1.0-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
maven {
credentials {
username "$nexusUser"
password "$nexusPass"
}
url "$nexusUrl"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "$nexusSnapshotUrl") {
authentication(userName: "$nexusUser", password: "$nexusPass")
}
}
}
}
}
ext.comlib = [ // Groovy map literal
junit3: "junit:junit:3.8",
junit4: "junit:junit:4.9",
spring_core: "org.springframework:spring-core:3.1",
hibernate_validator : "org.hibernate:hibernate-validator:5.1.3.Final",
spring_core : "org.springframework.security:spring-security-core:4.0.2.RELEASE",
spring_security_web: "org.springframework.security:spring-security-web:4.0.2.RELEASE",
spring_security_config: "org.springframework.security:spring-security-config:4.0.2.RELEASE",
spring_boot_starter_test: "org.springframework.boot:spring-boot-starter-test:1.2.5.RELEASE",
spring_boot_starter_actuator: "org.springframework.boot:spring-boot-starter-actuator:1.2.5.RELEASE",
spring_boot_plugin_gradle: "org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE",
asciidoctor_gradle_plugin: "org.asciidoctor:asciidoctor-gradle-plugin:1.5.1",
asciidoctor_pdf_plugin: "org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.9"/*,
sl4j_api: "org.slf4j:slf4j-api:1.7.12",
sl4j_log4j: "org.slf4j:slf4j-log4j12:1.7.12",
logback_classic: "ch.qos.logback:logback-classic:1.1.3",
logback_core: "ch.qos.logback:logback-core:1.1.3"*/
]
build file in sub module
apply plugin: 'spring-boot'
group = 'com.group.id'
apply from: "../build.gradle"
apply plugin: 'org.asciidoctor.gradle.asciidoctor'
apply plugin: 'war'
description = 'module name'
dependencies {
compile "someothermodule:commonapi:1.0.0-SNAPSHOT"
compile "io.springfox:springfox-swagger2:2.0.1"
compile project(':dependingproject1:dependingproject2')
compile comlib.spring_boot_starter_actuator
compile comlib.spring_core
compile comlib.spring_security_web
compile comlib.spring_security_config
testCompile(comlib.spring_boot_starter_test) {
exclude(module: 'commons-logging')
}
testCompile comlib.junit4
providedCompile comlib_app.spring_boot_plugin_tomcat
testCompile "io.springfox:springfox-staticdocs:2.0.3"
testCompile "org.springframework:spring-test:4.1.7.RELEASE"
}
ext {
swaggerOutputDir = file("src/docs/asciidoc/generated")
asciiDocOutputDir = file("${buildDir}/asciidoc")
}
test {
systemProperty 'org.springframework.restdocs.outputDir', asciiDocOutputDir
systemProperty 'io.springfox.staticdocs.outputDir', swaggerOutputDir
}
//spring boot plugin
buildscript {
repositories {
maven {
credentials {
username "$nexusUser"
password "$nexusPass"
}
url "$nexusCentral"
}
maven {
credentials {
username "$nexusUser"
password "$nexusPass"
}
url "$nexusThirdParty"
}
}
dependencies {
classpath(comlib.spring_boot_plugin_gradle)
}
}
Included the following code snippet in my gradle file
[distZip, distTar].each { task -> configurations.archives.artifacts.removeAll
{ it.class.simpleName == "ArchivePublishArtifact" && it.archiveTask == task }
task.enabled = false
}
For more details refer this link. Its issue with spring boot plugin.
arjuncc's solution doesn't seem to work on Gradle 4.10.2, so here's one that works and uses public APIs, hopefully it will continue to work.
configurations.archives.artifacts.removeAll {
// exclude from the archives configuration all artifacts that were generated by distZip & distTar
def depTasks = it.getBuildDependencies().getDependencies()
depTasks.contains(distZip) || depTasks.contains(distTar)
}
More or less the same as ajuncc's solution, but perhaps a bit more simple. Remove all .tar artifacts from the archives configuration:
configurations.archives.artifacts.removeAll {PublishArtifact publishArtifact -> publishArtifact.type == 'tar'}

Resources