How to upload an existing collection of 3rd-party Jars to a Maven server in Gradle? - maven

How can I upload a collection of existing Jars to a Maven repository? The Jars are built from an ANT Task imported to Gradle, and used as a dependency to my task... The Jars don't have version tag, so they should ALL receive the same version number when they are uploaded...
apply plugin: 'java'
apply plugin: 'maven'
version = "6.1.1"
group = "com.oahu"
ant.importBuild "$projectDir/tools/ant/package.xml"
uploadArchives(dependsOn: ["oahu-jar", "client-sdk-jar", "common-jar"]) << {
// the dependencies will generate "oahu.jar", "oahu_client_sdk.jar", "common.jar"
// UPLOAD THE DEPENDENCIES LISTED ABOVE LOCATED AT the subdirectory "build/"
description = "Uploads the generated jar ${archivesBaseName}-${version}.jar to ${cn_mvn_serverUrl}"
repositories.mavenDeployer {
repository(url: "${cn_mvn_releaseUrl}") {
authentication(userName: "${cn_mvn_username}", password: "${cn_mvn_password}")
}
}
}
The tasks "oahu-jar", "client-sdk-jar", "common-jar" are the ones imported from ANT... I have the Maven repositories configuration already working from another project... But the Maven plugin uploads the Jar generated by the Jar task from the Java plugin... Considering the imported ANT tasks generates:
build.gradle
src
build
|-"oahu.jar"
|-"oahu_client_sdk.jar"
|-"common.jar"
The result of this should be the upload of those Jars with the given version...
"oahu-6.1.1.jar", "oahu_client_sdk-6.1.1.jar", "common-6.1.1.jar"... all uploaded to the Maven repository...
Add sourceSets? Configuration? Artifacts?

currently that is not explicitly supported by gradle so you have to do some scripting for that. Based on your snippet above, I've created a sample snippet, that should be easy to adapt:
apply plugin:'java'
apply plugin:'maven'
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact
version = "6.1.1"
group = "com.oahu"
ant.importBuild "$projectDir/tools/ant/package.xml"
// a list of the ant tasks that create a jar
// I assumed the following convention:
// ant task named "SampleAntJar-jar" creates the jar "build/SampleAntJar.jar"
def antJarTasks = ["SampleAntJar-jar", "SecondSampleAntJar-jar"]
artifacts{
//for each ant task add a defaultpublishArtifact to the archives configuration
antJarTasks.each{ taskName ->
def artifactName = taskName - '-jar'
archives new DefaultPublishArtifact(artifactName, "jar", "jar", null, new
Date(), new File("$buildDir", "${artifactName}.jar"))
}
}
uploadArchives(){
dependsOn: antJarTasks
repositories {
mavenDeployer {
repository(url: "file://{'/Users/Rene/.m2/repository/'}")
antJarTasks.each{ antJarTask ->
antJarName = antJarTask - "-jar"
addFilter(antJarName) {artifact, file ->
artifact.name == antJarName
}
pom(antJarName).artifactId = antJarName
}
}
}
}
regards,
René

Related

Artifact is not published using gradle and artifactory plugin

I'm trying to upload a simple zip to artifactory using gradle. This is my script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def user = "$System.env.ARTIFACTORY_USER"
def pass = "$System.env.ARTIFACTORY_PASSWORD"
task createArchive(type: Zip){
baseName 'myzip'
destinationDir(new File('target'))
from '.'
exclude 'target'
exclude '.gradle'
exclude 'build.gradle'
exclude 'README.md'
doFirst{
new File('target').mkdirs()
}
}
task clean(type: Delete){
delete 'target'
}
publishing {
publications {
customArtifact(MavenPublication) {
artifact createArchive
}
}
}
artifactory {
publish {
contextUrl = 'http://myrepo/artifactory'
repository {
repoKey = "myrepo-tools"
username = user
password = pass
}
publications ('customArtifact')
}
}
The code seems to work fine but actually only the build info are published. The zip is not:
Deploying build descriptor to: http://myrepo/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://myrepo/artifactory/webapp/builds/myproject/1503338121156
My code does not seem different from the one published here: How to build Groovy JAR w/ Gradle and publish it to in-house repo (which was pointed as reference in question nothing published to artifactory using gradle artifactory plugin) but I'm not sure that is still valid since is quite old.
The main difference is that almost all the examples are using java plugin so they have the from components.java line in the publications. In my case I cannot use it as I have a custom zip.. but I can't find what else can be changed.
Thanks,
Michele.

Not Publishing the mentioned file(war/tar/zip) to artifactory in gradle script

I wrote a gradle script where I am creating the zip and war file and then I need to upload/publish it to the artifactory but the issue is I specified the war file in my artifact task even after that it is publishing everything to the artifactory zip,tar and war instead of only war file.
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'distribution'
//-- set the group for publishing
group = 'com.xxx.discovery'
/**
* Initializing GAVC settings
*/
def buildProperties = new Properties()
file("version.properties").withInputStream {
stream -> buildProperties.load(stream)
}
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.coveryadBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.coveryadBuildVersion
println "${version}"
//name is set in the settings.gradle file
group = "com.aaa.covery"
version = buildProperties.discoveryadBuildVersion
println "Building ${project.group}:${project.name}:${project.version}"
repositories {
maven {
url "http://cxxxxt.tshaaaaa.tho.com:9000/artifactory/libselease"
}
maven {
url "http://cxxxxt.tshaaa.tho.com:9000/artifactory/cache"
}
}
dependencies {
compile ([
"com.uters.omni:HermesSessionAPI:1.2",
"com.uters.proxy:ProxyResources:1.1",
"com.uters.omni:SeshataDirectory:1.0.1" ,
"com.euters.omni:CybeleInfrastructure:1.1.2",
"com.euters:JSONBus:1.4.1",
"javaee:javaee-api:5"
])
}
distributions {
main {
contents {
from {
war.outputs
}
}
}
}
// for publishing to artifactory
artifacts {
archives war
}
According to gradle distribution plugin documentation:
All of the files in the “src/$distribution.name/dist” directory will automatically be included in the distribution.
And also,
The distribution plugin adds the distribution archives as candidate for default publishing artifacts.
In other words, by default all the files will be published so this explains the behavior you're experiencing.
What you can probably do in order to workaround this behavior is to define the contents copySpec more accurately by explicitly exclude the unwanted files, i.e.:
distributions {
main {
contents {
exclude('**/.zip')
exclude('**/.tar')
from {
war.outputs
}
}
}
}
Note that I didn't try the above by myself though so some fine tuning might be needed. However I believe that you can find the data you need in the CopySpec Interface documentation

Using War plugin but without creating an archive

I'm trying to get Gradle to handle the deployment of a very large Web application. In the past when we used Ant, instead of creating a very large .war file, we would simply assemble all the code in one folder--libraries, .jsp's etc--and then scp them to the deployment destination. This would speed deployment since we would be moving only the files that changed.
I'm having trouble trying to do this with Gradle, however. Using the War plugin creates an actual .war file, which we don't want. I've tried simply creating a task that depends on 'classes' and that generates the necessary classes and resources folders. However, where are the library dependencies? How can I get this all in one place so I can do an scp?
Current build:
apply plugin: "java"
apply plugin: "maven"
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.6
targetCompatibility = 1.6
repositories {
maven {
url 'http://buildserver/artifactory/repo'
}
}
sourceSets {
main {
java {
srcDir 'src'
}
resources {
srcDir 'src'
}
}
}
webAppDirName = 'web'
dependencies {
compile 'antlr:antlr:2.7.6'
compile 'antlr:antlr:2.7.7'
*** etc ***
}
task deploystage(dependsOn: 'classes') << {
println 'assemble and scp code here'
}
your right Opal ... no easynway to use the War plugin for this ... although it would be nice if it had a createExplodedWar option. I just used the Jar plugin methods.
The .jars were available, along with other things in war.classpath.files:
gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
project.ext.set("allclasspathjars", files(war.classpath.files))
}
I then when through these to separate out the .jars from the other stuff:
task deployJars(dependsOn: 'classes') << {
FileUtils.cleanDirectory(new File("web/WEB-INF/lib/"))
project.allclasspathjars.each {File file ->
if(file.name.endsWith(".jar")) {
println "Including .jar: " + file.name
org.apache.commons.io.FileUtils.copyFile(file, new File("web/WEB-INF/lib/" + file.name))
}
}
After this I could copy all the classes & resources into the .war structure:
task createExplodedWar(type: Copy, dependsOn: 'deployJars') {
from 'build/classes/main'
from 'build/resources/main'
into 'web/WEB-INF/classes'
}
Finally used Gradle SSH Plugin to push it up to tomcat and restart the server.
All good.

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' )
}
}
}
}
}
}

How to generate multiple jar files with gradle's java plugin

I have a multi-project gradle build using the java plugin setup as follows:
myProj/
settings.gradle
build.gradle
util/
build.gradle
In my util project, I would like to generate 2 jars... one for packageA and one for packageB. I'm a noob with gradle so any help here would be much appreciated. Here are my settings and gradle files:
myProj/settings.gradle
include 'util'
myProj/build.gradle
subprojects {
apply plugin: 'java'
repositories {
maven {
url "http://mymavenurl"
}
}
sourceSets {
main {
java {
srcDir 'src/java'
}
}
}
}
myProj/util/build.gradle
dependencies {
.
.
.
}
jar {
baseName = 'packageA'
includes = ['com/mycomp/packageA']
}
task packageBJar(type: Jar) {
dependsOn classes
includes = ['com/mycomp/packageB']
baseName = 'packageB'
}
When I try to build my project here is the output:
:util:compileJava
:util:processResources UP-TO-DATE
:util:classes
:util:jar
:util:assemble
:util:compileTestJava UP-TO-DATE
:util:processTestResources UP-TO-DATE
:util:testClasses UP-TO-DATE
:util:test
:util:check
:util:build
I would hope to see :util:packageBJar after classes, but I'm not having any luck.
One way is to declare packageBJar as an artifact of, say, the archives configuration:
artifacts {
archives packageBJar
}
Now gradle assemble, and therefore also gradle build, will produce packageBJar.

Resources