I am using Gradle to build my project.Script is working fine and it generates output file using task "installApp". The problem here is output folder is same as the project folder name and the executable file also creates with project name. for e.g. my project name is com.eclipse.spring.shell so the output of gradle script creates folder "com.eclipse.spring.shell" and name of the executable is also "com.eclipse.spring.shell".
SO my question is can i customize the output folder name as well executable file name in the script?
my gradle script looks like mentioned below:
description = '
Command Line Interface'
apply plugin: 'base'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
dependencies {
compile project(':com.eclipse.spring.Framework'), project(':com.eclipse.spring.User'), project(':com.eclipse.spring.Manager'), project(':com.eclipse.spring.Test')
}
mainClassName = "com.eclipse.spring.Shell.RvsBootstrap"
defaultTasks 'installApp'
run {
standardInput = System.in
}
task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
gradleVersion = '1.2'
}
You can set the output folder name under installApp and the applicationName under startScripts.
Example:
installApp {
destinationDir = file('build/install/'+'YourFolderName')
}
startScripts {
applicationName = 'YourApplicationName'
}
Related
I have the following build.gradle file. Currently the task generateSources runs every time gradle is executed ("BUILD SUCCESSFUL"). I would instead like it to only execute when the build.gradle file itself changes, so that the build is an incremental build ("UP-TO-DATE")
i.e. I want it's "input" to be the "build.gradle" itself.
How do I do this?
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
// inputs = ????
// onlyIf ???
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources
(The code above is simplified to the bare minimum. In reality the task generate some files, and they are configured properly in Task.output)
There are two ways as I know, finalizedBy and dependsOn
The finalizedBy means do this after this task
tasks.named("build") { finalizedBy("myTaskName") }
And the dependsOn means that after build do this task
tasks.named("myTaskName") { dependsOn("build") }
More information about tasks on the official docs
It turns out you can just put an input-dependency on the project file object itself with something like inputs.file project.buildFile.
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
inputs.file project.buildFile // only rebuild when _this_ build.gradle changes
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources
I have done below profiling for copying the file from a specific environment directory mentioned during the build command (gradle clean build -x test -Penv=prod) and delete the environment directory after it.
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'application'
task copyProfile(dependsOn:"deleteProfiles") {
def env = project.hasProperty('env') ? env : 'dev'
sourceSets.main.resources.srcDir "src/main/resources/environment/$env"
}
task deleteProfiles(type:Delete) {
delete "src/main/resources/environment"
}
tasks.clean.dependsOn(tasks.deleteProfiles)
the deleteProfiles task is working, but copyProfile task
sourceSets.main.resources.srcDir "src/main/resources/environment/$env"
i.e. copying files from $env directory and put in src/main/resources is not working. Can any one suggest me why it is not working
You didn't tell the task to copy anything...
Change your task to this:
task copyProfile(dependsOn:"deleteProfiles") {
def env = project.findProperty('env') ?: 'dev'
doLast {
project.copy {
from "src/main/resources/environment/$env"
into sourceSets.main.resources.srcDir
}
}
}
See Project.copy(...)
This is about as basic as it could be... but I haven't found the answer either here or at gradle.org or generally. NB Gradle 4.4.1, Groovy 2.4 (used by Eclipse-Gradle) or 2.6 (command line). Java 8.
In my build.gradle I have the application plugin and have set the main class:
apply plugin: 'application'
mainClassName = "core.ConsoleHandler"
... including this plugin adds 2 "dependency tasks" (if that's the right term: i.e. build is now "dependent" on them): taskZip and taskTar.
Here we see that taskZip is of type Zip... and looking up the doc for this subclass of Task here we see that one of the properties of such a Task is destinationDir... i.e. where the .zip file ends up.
All I want to do is set this to a specific directory.
I've tried (in build.gradle) things like
task application.taskZip {
destinationDir = 'D:/bobble'
}
and
taskZip {
destinationDir = 'D:/bobble'
}
and
destinationDir = 'D:/bobble'
These all produce fatal errors. What should I be doing?
It's distZip not taskZip
apply plugin: 'application'
distZip {
destinationDir = file('D:/bobble')
}
destinationDir takes a File parameter
Try something like that :
tasks.withType(taskZip) {
destinationDir = file("D:/bobble")
}
I've a single gradle file where I'm applying eclipse plugin:
plugins {
id 'java'
id 'eclipse'
}
repositories {
mavenCentral()
}
dependencies {
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.codehaus.jettison:jettison:1.3.7'
compile 'org.apache.directory.api:api-all:1.0.0-M30'
compile 'com.whalin:Memcached-Java-Client:3.0.2'
compile 'org.mongodb:mongo-java-driver:2.14.3'
compile 'commons-configuration:commons-configuration:1.10'
}
task wrapper(type: Wrapper) {
gradleVersion = '3.1'
}
manifest {
attributes 'Implementation-Title': '---', 'Implementation-Version': 0.1
}
All my source files are under src/main/java. When I perform gradle eclipse it generates me eclipse artifacts. Then, I import this project using Import > General > Existing project into workspace. Nevertheless, my source folders are not set such as source folder into imported project.
Porject structure:
workspace
└───project
└───src
└───main
├───java
└───resources
I'd also like to set other parameters like output folder, java compliance compiler...
Any ideas?
On the official site there is a solution of setting the output folder.
apply plugin: 'java'
sourceSets {
main {
//if you truly want to override the defaults:
output.resourcesDir = 'out/res'
output.classesDir = 'out/bin'
}
}
I hope it helps.
Hibernate-cfg.xml not added to war classes folder.I am using below script to deploy web applcation to tomcat.After copy, when i am starting tomcat , gettign below error
eNotFoundException: class path resource [hibernate.cfg.xml] cannot be resolved URL because it does not exist
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.7
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile("javax.servlet:jstl:1.2")
compile("org.springframework:spring-context:4.0.3.RELEASE")
compile("org.springframework:spring-webmvc:4.0.3.RELEASE")
compile("org.springframework:spring-web:4.0.3.RELEASE")
compile("org.springframework:spring-aop:4.0.3.RELEASE")
compile("org.springframework:spring-aspects:4.0.3.RELEASE")
compile("org.springframework:spring-beans:4.0.3.RELEASE")
compile("org.springframework:spring-core:4.0.3.RELEASE")
compile("org.springframework:spring-expression:4.0.3.RELEASE")
compile("org.springframework:spring-jdbc:4.0.3.RELEASE")
compile("org.springframework:spring-orm:4.0.3.RELEASE")
compile("org.eclipse.persistence:javax.persistence:2.0.0")
compile("antlr:antlr:2.7.7")
compile("commons-logging:commons-logging:1.1.1")
compile("org.hibernate:hibernate-commons-annotations:3.2.0.Final")
compile("org.hibernate:hibernate-core:4.3.5.Final")
compile("org.apache.derby:derbyclient:10.12.1.1")
compile("javax.validation:validation-api:1.0.0.GA")
compile("org.slf4j:slf4j-api:1.7.5")
}
task deploy (dependsOn: war){
copy {
from "build/libs"
into "C:/soft/apache-tomcat-7.0.67/webapps"
include "*.war"
}
}
/*task startTomcat(dependsOn:deploy,type:Exec) {
workingDir "C:/mdi/soft/apache-tomcat-7.0.67/bin"
commandLine 'cmd', '/c', 'startup.bat'
}*/
task startTomcat << {
def processBuilder = new ProcessBuilder(['cmd','/c','startup.bat'])
processBuilder.directory(new File("C:/soft/apache-tomcat-7.0.67/bin"))
processBuilder.start()
}
// Set source directory
// War file name
war
{
war.baseName = 'userregisteration'
project.webAppDirName = 'WebContent'
sourceSets{
main {
java {
srcDir 'src'
}
}
}
}
In your war task add a from closure:
from(<directory containing Hibernate-cfg.xml>){
into <'directory in the war in which you'd like the file to be placed'>
include 'Hibernate-cfg.xml'
}
This is also pretty basic. I'd recommend perhaps reviewing the Gradle manual again to gain a better understanding of working with files.