I have a spring context file and a property file located in my project root.
When running gradle distZip i get all the sources included as well as the libraries, but how do I also include the two files?
These are the simplest working build files that solves the problem. You can easily make use of contents specification in your build. More information about CopySpec.
Using distribution plugin
apply plugin: 'distribution'
distributions {
main {
contents {
from "$projectDir"
into 'doc'
include 'README'
}
}
}
Using application plugin
apply plugin: 'application'
mainClassName = 'Main'
applicationDistribution.from("$projectDir") {
into 'doc'
include 'README'
}
Related
I have a project corehibernate and a project coregeneral. corehibernate is dependent on coregeneral. I need the jar file of coregeneral to be bundled along with the corehibernate jar. I tried various versions of the build.gradle thing, nothing worked.
I tried compile files("../coregeneral/build/libs/coregeneral.jar")
This version of fatJar too does not work.
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile (':coregeneral')
testCompile 'junit:junit:4.12'
}
jar {
baseName='corehibernate'
from ('bin')
}
task fatJar(type: Jar, dependsOn: jar) {
baseName = project.name + '-fat'
}
There are two basic ways how to bundle projects together. The first would be to use application plugin which creates a zip with scripts that will also execute your application and bundle all jars by default. Second way is to use distribution plugin and define the final archive yourself (zip or tar).
Here is a sample project using the application plugin:
settings.gradle
rootProject.name = 'root'
include 'partone', 'parttwo'
build.gradle
subprojects {
apply plugin: 'java'
}
partone/build.gradle - this one is empty
parttwo/build.gradle
apply plugin: 'application'
mainClassName = 'Hello'
dependencies {
compile project (':partone')
}
Give that both projects actually have some content (classes), when you run gradle :projecttwo:build it will generate a zip file with executable scripts and both jars bundled inside.
If you prefer to use distribution plugin, change the parttwo/build.gradle to:
apply plugin: 'distribution'
distributions {
main {
contents {
from jar
from (project.configurations.runtime)
}
}
}
dependencies {
compile project (':partone')
}
And again run gradle :parttwo:build. It will create a zip file that contains both jars.
I have a project which is splitted into two subprojects.
/project
/sub-project-a (backend with JAVA source which is compiled into JAR file)
/sub-project-b (frontend sources which are compiled with grunt via gradle call)
build.gradle
settings.gradle (contains include 'sub-project-a', 'sub-project-b')
My Question is how can I create a War file with sub-projects and external lib dependencies? The following code snipped is my current build.gradle:
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile project(':sub-project-a')
compile project(':sub-project-b')
compile 'com.google.code.gson:gson:2.2.4'
}
task copy(type: Copy) {
from 'sub-project-a/build', 'sub-project-b/build'
into 'build'
}
build.dependsOn clean, copy
war {
archiveName 'project.war'
}
One detail is important. The java context listener (deep inside project code) work with compiled backend as jar file from WEB-INF/lib folder. This means that all class files can't be easily used from WEB-INF/classes folder.
As you can see I played with dependencies and a custom copy task. I'm not sure what is right gradle way. How should I do this?
SOLUTION
Define with war.from methode, where you get your static sources.
gradle docu
from(sourcePaths) -
Specifies source files or directories for a copy. The given paths are
evaluated as per Project.files().
My changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
SOLUTION (for cleanly closing this question) shamefully taken from the question's originator ;-)
Define subproject dependencies with the "war.from" method, where you get your static sources.
gradle documentation excerpt: from(sourcePaths) - Specifies source files or directories
for a copy. The given paths are evaluated as per Project.files().
Ronny's changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
I am trying to use Cascading in my Hadoop project. I am trying to implement first example given in Enterprise Data Workflows with Cascading book. I have written java class which contains Cascading related code and I have another build.graddle file which is supposed to compile that java class and build jar file out of it.
My folder structure is as follows :
main_folder
impatient
Main.java
build.gradle
My build.gradle file looks as below :
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
archivesBaseName = 'impatient'
repositories {
mavenLocal()
mavenCentral()
mavenRepo name: 'conjars', url: 'http://conjars.org/repo/'
}
ext.cascadingVersion = '2.1.0'
dependencies {
compile( group: 'cascading', name: 'cascading-core', version: cascadingVersion )
compile( group: 'cascading', name: 'cascading-hadoop', version: cascadingVersion )
}
jar {
description = "Assembles a Hadoop ready jar file"
doFirst {
into( 'lib' ) {
from configurations.compile
}
}
manifest {
attributes( "Main-Class": "impatient/Main" )
}
}
When I run gradle clean jar command from command prompt, I get build successful message. I tried to run this jar file using
hadoop jar impatient.jar <input file path> <output file path>
command but then it gives me Exception in thread "main" java.lang.ClassNotFoundException: impatient.Main exception.
So I checked contentes of jar file and found that that jar does not contain impatient/Main.class file.
Please note that I do not know anything about gradle.
Request someone to please tell me if there is anything wrong with gradle script or I am making some mistake.
Thanks !!!
Move your source file to
main_folder/impatient/src/main/java/Main.java
but leave build.gradle file where it is.
By default, Gradle uses src/main/java and src/test/java to look for production and test java sources (relative to root folder, which is impatient in your case)
I have development related directory src/main/resources/certs/test which is needed for one external library. This has some cert files which are not needed in production build.
At the moment I exclude everything under that directory with following block in build.gradle:
sourceSets {
main {
resources {
exclude '**/test/*'
}
}
}
This does it job well, but leaves ugly empty directory test lying there. What are my options to not include this directory in final war?
I've tried excluding '**/test', but it doesn't work at all.
I use war plugin and Gradle 1.2
Using Gradle 1.1, this works for me:
apply plugin: 'war'
sourceSets {
main {
resources {
exclude '**/test/*'
exclude 'certs/test'
}
}
}
I had a similar problem with production files in a JAR file (though mine were not test files). I solved it with the following:
jar {
exclude ("DIRECTORY-TO-EXCLUDE/**")
}
e.g.
jar {
exclude ("test/**")
}
A common projet layout is to put test files under the test source set, this way you don't have to exclude them from the main source set.
From the Gradle documentation, the default project layout is like this:
src/main/java Production Java source
src/main/resources Production resources
src/test/java Test Java source
src/test/resources Test resources
I am trying with Gradle first time. I am trying with a maven java project to compile and create a jar file. It is compiling and creating the jar file in build/libs directory as
trunk-XXXVERSION-SNAPSHOT.jar
I am running gradle build file from trunk directory of this maven java project.
I want to get the project name (for ex: project1) in the jar file name, something like
project1-XXXVERSION-SNAPSHOT.jar
in build/libs directory.
Please suggest.
Here is the directory structure:
trunk
˪ build
˪ libs
˪ project1-1.0-SNAPSHOT.jar
˪ build.gradle
Include the following in build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
archivesBaseName = 'project1'
version = '1.0-SNAPSHOT'
group = 'example'
This will produce the correct ZIPs, POMs and JARs.
Additionally, try setting:
archivesBaseName = 'project1'
or (deprecated):
jar.baseName = 'project1'
The default project name is taken from the directory the project is stored in. Instead of changing the naming of the jar explicitly you should set the project name correct for your build. At the moment this is not possible within the build.gradle file. Instead, you have to create a settings.gradle file in your root directory. This settings.gradle file should have this one liner included:
rootProject.name = 'project1'
I recently migrated to gradle 4.6 (from 3. something) and the
jar {
baseName = 'myjarname'
}
stopped working, gradle named my jar from the folder name.
So I switched to archivesBaseName = 'myjarname' which works.
Maybe this helps somebody else too.
If you has some submobule, you can use in build.gradle (for jar)
configurations {
jar.archiveName = 'submodule-jar.jar'
}
In Kotlin DSL you can also use:
tasks.jar {
archiveFileName.set("app.jar")
}
With Spring boot and Kotlin DSL you can use:
tasks {
bootJar {
archiveFileName.set("app.jar")
}
}
Currently using Kotlin as Gradle DSL. Following statement works for me:
tasks.withType<AbstractArchiveTask> {
setProperty("archiveFileName", "hello-world.jar")
}
It works on Spring Boot executable jars as well.
If you want to keep version numbers:
tasks.withType<AbstractArchiveTask> {
setProperty("archiveBaseName", "hello-world")
}
It will produce something like hello-world-1.2.3.jar
If you are using a newer Gradle version, baseName, archiveName will now be deprecated. Instead, use something like
jar {
archivesBaseName = 'project1'
archiveVersion = '1.0-SNAPSHOT'
}
if you want to append a date to the jar file name, you can do it like this:
jar {
baseName +='_' +new java.text.SimpleDateFormat("dd_MM_yyyy").format(new java.util.Date())
println(baseName) // just to verify
which results in <basename>_07_05_2020.jar
You have to remove the 'version' tag in your build.gradle file!
It can works in Gradle 7.5.1 with Groove DSL:
jar {
archiveFileName = "name.jar"
}
If you are using Kotlin DSL:
tasks.withType<Jar> {
archiveFileName.set("name.jar")
}