Gradle: How do I copy POM files to WAR? - gradle

The WEB-INF\lib folder in our WAR file should serve as a flatDir repository for our users. I presume I need to supply a POM file for each JAR, so that transitive dependencies would be resolved.
How do I copy POM files for each JAR to WEB-INF\lib folder in my WAR file?
My root project script:
apply plugin: 'war'
jar.enabled = false
war {
dependencies {
subprojects.each { runtime it }
providedCompile servlet
}
}

Please see the flatDir documentation which states
Note that this type of repository does not support any meta-data formats like Ivy XML or Maven POM files.
If you want to download all dependencies (and pom files) to a maven directory structure you can use this gist. To download javadocs and sources too see this answer

Related

How to publish an obfuscated jar file and pom to Nexus repository with gradle

I need to publish a couple of obfuscated jar files with their POM (before obfuscation) to a Nexus repository with Gradle.
The problem is if I select components.java then the non-obfuscated jar file is deployed.
I am using yGuard for obfuscation, and now in my build file I have created pom file, clean jar file and obfuscated jar file, but cannot find a way to publish the pom and obfuscated jar file together to our Nexus repository.
By the way the project is a multi module java-library project.
In my main build.gradle file I have something like
configure(subprojects) {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
jar {
archiveFileName = "${project.name}-${project.version}-clean.jar"
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForCleanPublication }
rename '.*', 'pom.xml'
}
}
publishing {
publications {
clean(MavenPublication) {
from components.java
withBuildIdentifier()
}
}
repositories {
maven {
name 'nexus'
url 'SOME URL'
allowInsecureProtocol = true
credentials {
username 'someUserName'
password 'somePassword'
}
}
}
}
}
so the pom is created according to module dependencies, but now I cannot publish the obfuscated jar file with same POM file.
Notes: The obfuscated jar files are generated in same folder as 'build\lib' without '-clean.jar` suffix.
Notes: when I remove from components.java and add my own artifact (obfuscated file) the generated pom won't have dependencies, and I couldn't find a way to override the jar file added to publication.

gradle war: how to build jar, not classes

Gradle war plugin: how to build a jar and add it to war?
projectRoot/
src/main/java
src/main/resources
src/main/webapp
build a jar (foo.jar) from the java source code and resources.
add the jar under the WEB-INF/lib of the war.
WEB-INF/lib/foo.jar
The war task will not build a jar by default, and add all java classes and resources under WEB-INF/classes.
UPDATE
The War plugin extends the Java plugin to add support for assembling web
application WAR files. It disables the default JAR archive generation of the
Java plugin and adds a default WAR archive task.
There is a way to enable the Jar generation and let task war depends on task jar?
not sure if eastwater still needs the answer, hope others with the same problem will find this helpful
you can add/configure the war task in build.gradle
war {
classpath = classpath - sourceSets.main.output
from (jar) {
into 'WEB-INF/lib'
}
}
once build succeed, in build/libs folder you'll see the generated jar and the war containing the generated jar instead of classes
Add those into your root build.gradle
plugins {
id 'java'
id 'idea'
}
jar{
String somestr=''
configurations.runtime.each{somestr=somestr+" lib\\"+it.name}
manifest{
attributes 'Main-Class':'your_class_name'
attributes 'Class-Path':somestr
}
}
task copyJar(type:Copy){
from configurations.runtime
into ('build/libs/lib')
}
task release(type: Copy,dependsOn:[build,copyJar]){
}
add finally run this command
gradle release

How do I define a simple Gradle project with only a single jar file?

I have a Gradle project that depends on an external jar file. Currently I'm defining the dependency like this:
dependencies {
compile files('/path/to/my/jar/library.jar')
}
However I want to include it as a project dependency instead, like this:
dependencies {
compile project(':whatGoesHere?')
}
I assume I need to define a new Gradle project that contains the jar file but I don't know how to do this. I'm wondering about things like:
Do I just need to create a new build.gradle or are there more steps?
What would go in the build.gradle file?
Assume the new project contains nothing but the jar file (since it does). Also assume I know almost nothing about Gradle (because I don't!).
P.S. If it matters, this is an Android Gradle project.
As a roundup for our discussion, I'll bring simple example of "build.gradle" file, using maven local and maven central repositories:
apply plugin: 'maven'
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'commons-io:commons-io:2.4'
testCompile 'junit:junit:4.11'
}
Explanation:
"apply plugin: 'maven'" enables maven plugin, which is needed for dependency download.
"apply plugin: 'java'" enables java compilation tasks for your project.
"repositories" declares one or more repositories (maven or ivy), from where artifacts (jar libraries) will be downloaded.
"mavenLocal" refers to so-called local maven repository, which is located in "~/.m2/repository" folder on your computer. local maven repository effectively caches external repositories, but it also allows installation of local-only artifacts.
"mavenCentral" refers to maven central.
"dependencies" lists your project dependencies, either other projects or artifacts (jars).
"compile" is a configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during compilation phase".
"testCompile" is another configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during test phase".
'commons-io:commons-io:2.4' is "coordinates" of the artifact within maven repository, in form group:name:version.
You can search for well-known java libraries at address: http://mvnrepository.com/ and then include their coordinates in "build.gradle". You don't need to download anything - gradle does it for you automatically.

Gradle Dependency loading from maven

I am new to gradle.
I have seen some examples about java dependency like the following example but my project will be simply a zip file.
I just want to download the zip file.
apply plugin: 'java'
dependencies {
compile 'commons-lang:commons-lang:2.6'
}
In the above example, it will automatically download the jar file. But it doesn't download my zip file if my maven repositories contains zip that mentioned in the pom.xml about that package.
Questions:
What is the flow when depend on a maven repository? It will first read the pom.xml and then download the zip file?
How to dynamically load the dependency? e.g 'commons-lang:commons-lang:2.6' will have dependency of 'commons-lang:en:1.0" in the pom.xml. How to make it automatically load and loop the dependency list?
Thanks all
I have tried the follwoing script but it gives me error on compile but I have apply the java plugin
My gradle file
apply plugin: 'java'
repositories {
mavenLocal()
maven {
url "http://nexus.com/myrepo/"
}
}
dependencies {
compile 'com.a.b:projectA:2.0#zip'
}
I can run without problem that files downloaded are inside .m2
Question about the transitive dependency
I have the pom.xml like this. But it is unable to load the dependency one. It will directly go to the new pom.xml first or download zip directly if i mention sth like this?
<dependencies>
<dependency>
<groupId>com.a.b.c</groupId>
<artifactId>base</artifactId>
<version>1.2</version>
<type>zip</type>
</dependency>
</dependencies>
When declaring a dependency and a maven repository, this maven repository will be used to resolve the artifact. this means that usually first the metadata is read and then the artifact will be downloaded. If no repository is declared the resolution will fail early.
Having a dependency notation like yours:
dependencies {
compile 'commons-lang:commons-lang:2.6'
}
gradle resolves the default artifact of that dependency. If you want to resolve additional declared zip files from maven central, you have to use this notation
repositories{
mavenCentral()
}
dependencies {
compile 'commons-lang:commons-lang:2.6#zip'
}
As a default, the a dependency is transitive. This means, that if e.g 'commons-lang:commons-lang:2.6' has a dependency on 'commons-lang:en:1.0" in its pom.xml the commons-lang library (and again its transitive dependencies if any) is also resolved.
cheers,
René

Gradle way of handling maven non-lib artifact dependencies

I previously archived a zip artifact in our internal Maven repository.
In my Gradle buildscript I can reference the artifact as a dependency and can get a path to the artifact, using:
configurations{
resourceProperties
}
dependencies{
resourceProperties "$group:$name:$version"
}
... def path = configurations.resourceProperties.asPath
Unfortunately I get all the zip artifact's project dependencies appended on the path as well.
Is there another Gradle way to handle non-lib Maven artifacts required in my build?
You can include a file or directory as a dependency.
Try to look at this starting from par 45.4

Resources