Can I remove the 'jar' task in gradle build? - gradle

When I use the code below, a file of jar will generate after gradle build.
apply plugin 'java'
Is there any settings won't generate the file of jar??
I can write a custom plugins,but the code below was wrong.
dependencies {
compile project(':crm.common')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
I want find a way that donot generate the file of jar.
And can run compile in dependencies.
Is there any way can do that???

You can do that by 2 ways:
explicitly exclude the jar task from execution:
gradle build -x jar
disable the jar task in build.gradle:
apply plugin: 'java'
jar.enabled = false

This worked for me:
configurations.archives.with {
artifacts.remove artifacts.find { it.toString().contains("jar") }
}

Related

Adding sources for IntelliJ plugin via Gradle

I've setup an IntelliJ plugin using Gradle. My build.gradle file contains:
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.3.12'
}
//...
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
intellij {
version '2018.2.4'
plugins 'git4idea'
}
Is there any way how I can include/download the sources of the Git4Idea plugin to the project so that they are available automatically?
No, you can't. It'a bug. I filed it here on the plugin's issue tracker.

Unable to pass argument from gradle command line to Spring project (not Boot)

I am developing Spring project.
I would like to load credentials from the command line not storying them in the code. I'm trying to execute this gradle command
gradlew build -Dspring.datasource.username=tester
and when I startup the Spring project, the program stops on a breakpoint and I see whether variable is declared or not. I have tried using -P instead of -D but it still didn't help.
I deploy the spring app remotely using bmuschko plugin I've tried to use, but also without success. I checked in java code Properties by using System.getProperties() and Environment object supported by Spring.
gradlew cargoredeployremote -Dspring.datasource.username=tester
Application properties are loaded succesfully.
IMPORTANT: I saw many tutorials how to make it but using Spring Boot I use just selected components from Spring.
For instance: http://nixmash.com/post/passing-arguments-to-spring-boot - this doesn't work in my case because I have no bootRun task.
Any ideas? Am I missing something in my steps?
Here is my build.gradle
group 'example'
version '1.0.0'
apply plugin: 'application'
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'com.bmuschko.cargo'
apply plugin: 'org.liquibase.gradle'
compileJava.options.encoding = 'UTF-8'
mainClassName = 'api.optic.config.WebAppInitializer'
sourceCompatibility = 1.8
buildscript {
repositories{
jcenter()
mavenCentral()
}
dependencies{
classpath 'com.bmuschko:gradle-cargo-plugin:2.2.3'
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'
classpath 'org.liquibase:liquibase-core:3.4.1'
classpath "org.liquibase:liquibase-gradle-plugin:1.2.4"
classpath "mysql:mysql-connector-java:5.1.13"
}
}
project.ext {
springVersion = "4.3.6.RELEASE"
junitVersion = "5.0.0-RC3"
}
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework:spring-core:${springVersion}"
compile "org.springframework:spring-context:${springVersion}"
compile "org.springframework:spring-context-support:${springVersion}"
compile "org.springframework:spring-beans:${springVersion}"
compile "org.springframework:spring-web:${springVersion}"
compile "org.springframework:spring-webmvc:${springVersion}"
compile "org.springframework:spring-orm:${springVersion}"
compile "org.springframework:spring-oxm:${springVersion}"
compile "org.springframework:spring-jdbc:${springVersion}"
compile "org.springframework:spring-test:${springVersion}"
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.6'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.2'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.9.0.pr2'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8', version: '2.9.0.pr2'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.0.pr2'
compile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
cargo {
containerId = 'tomcat8x'
port = 8080
deployable {
context = 'example'
}
remote {
hostname = 'host.name.com'
username = 'tomcat'
password = 'pass'
}
Basically were 2 issues
1. Name mismatch: in application.properties the name inside the ${} was different than this provided from command-line
Solution:
application.properties
spring.datasource.username=${username}
and in gradle command-line
gradle build -Pusername=tester
2. Dot issue with gradle:
Can't put
gradle build -Pspring.datasource.username=tester
even if you have in application.properties
spring.datasource.username=${spring.datasource.username}
because you get an exception:
Execution failed for task ':processResources'.
Could not copy file '.\src\main\resources\application.properties' to '.\build\resources\main\application.properties'.
Solution:
Instead of dots use _ sign
gradle build -Pspring_datasource_username=tester
and in Application properties
spring.datasource.username=${spring_datasource_username}

Gradle dependency for compile time only and test

I am basically looking for a way to mimic the maven dependency provided. I am building a jar (an extension to a db driver), which depends on another jar (the db driver), but I do not want to include that jar.
I am able to use compileOnly to achieve that, however now the tests won't run or compile as the required jar is not included in tests.
I tried through the list of available dependencies like testCompile, however I could not find one that makes the jar available at compile time and when the tests run and compile.
How would I include that jar properly?
Edit: As requested, the build.gradle file:
group 'com.mygroup'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compileOnly "org.mongodb:mongodb-driver:3.3.0"
testCompile "org.mongodb:mongodb-driver:3.3.0"
}
Listing the dependency twice does work, however obviously is not a very nice solution
You can extend your testCompile configuration from the compileOnly configuration:
configurations {
testCompile.extendsFrom compileOnly
}
I use the following;
sourceSets {
// Make the compileOnly dependencies available when compiling/running tests
test.compileClasspath += configurations.compileOnly
test.runtimeClasspath += configurations.compileOnly
}
which is a line longer than the answer from tynn, but makes the intent clearer IMHO,

Skip building jar in Gradle groovy scripts project

I have a gradle groovy project where I only have groovy scripts that are not in a source dir, but a separate dir. Additionally I have groovy junit tests that test the scripts invoking them using groovy shell.
I have a gradle build that runs the tests, then zips the scripts into separate zip files and uploads them into maven repo. The problem is, that gradle also creates and uploads a jar file. Since there are no files in source dirs, the jar contains only a generated manifest file.
In reality I don't need the jar at all.Is it possible to configure gradle to not create a jar file for a groovy project?
I upload the artifacts using uploadArchives task.
My full gradle config:
group 'groupName'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task scriptsZip(type: Zip) {
from 'scripts'
}
artifacts {
archives file: scriptsZip.archivePath, type: 'zip', classifier: 'scripts', builtBy: scriptsZip
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://C:\\testRepo")
}
}
}
You can modify the jar task with an onlyIf condition, to skip building the jar when some condition is met (or not met)
jar {
onlyIf { /*some condition*/ }
}
In your case, it might make sense to check if there are any source files in your main sourceset:
jar {
onlyIf { !sourceSets.main.allSource.files.isEmpty() }
}

Gradle: run a specific java class

My gradle file looks like this:
apply plugin: 'java'
apply plugin:'application'
mainClassName = "com.example.Main"
project.buildDir = 'target'
version = '0.1'
jar{
destinationDir=project.buildDir
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
When I want to run, I type gradle run and it will execute com.example.Main class. I was wondering if there is a mechanism to execute specific class without changing the build.gradle. What I ultimately want is something similar to mvn exec:java -Dexec.mainClass="com.example.Main", where you can specify the main class.
You can set gradle project properties with -P, for example:
gradle run -PclassToExecute=com.myClass
and in the script:
mainClassName=classToExecute
If you change your build script to:
mainClassName = System.getProperty("exec.mainClass") ?: "default.Main"
You can execute a particular class with: gradle run -Dexec.mainClass=com.example.Main

Resources