gradle command line properties for subprojects - command-line

I have a multiproject gradle build whose top level has the following:
subprojects {
apply plugin: 'war'
httpPort = hasProperty('jettyPort') ? jettyPort.toInteger() : 8080
}
If I run from the command line gradle -PjettyPort=9000 war the call to hasProperty returns null, but if I move the hasProperty check outside the subprojects closure, then it returns true.
Is this the designed behavior or should I be able to access the properties from subprojects closure as I tried above.

Does this work?
subprojects {
apply plugin: 'war'
httpPort = project.hasProperty('jettyPort') ? jettyPort.toInteger() : 8080
}

Related

How do I run a scala application using gradle?

I added the scala plugin to gradle, but i don't how to run it. There's no run task when i create a scala project.
How do I run the scala project?
My gradle build script:
apply plugin: 'idea'
apply plugin: 'scala'
repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.13.1'
}
You need to provide the classpath as well. Change run task declaration to:
task run(type: JavaExec, dependsOn: classes) {
main = 'Demo'
classpath = sourceSets.main.runtimeClasspath
}
And it will work fine. Demo.

java-library does not expose scala classes

I have a gradle project that uses subprojects. One of them (common) uses an external library:
// common/build.gradle.kts
dependencies {
implementation("com.example:external-lib:1.2.3")
}
and has a Scala class that uses types in external-lib.
Another subproject references common:
// service/build.gradle.kts
dependencies {
implementation(project(":common"))
}
This works fine, but if I add the java-library plugin to common and switch implementation to api:
// common/build.gradle.kts
plugins {
`java-library`
}
dependencies {
api("com.example:external-lib:1.2.3")
}
then the code in service will see the types in external-lib (which is expected) but not the Scala class in common.
What am I doing wrong?
I used this for sub-projects to solve this issue:
subprojects {
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'scalaStyle'
apply plugin: 'idea'
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: scalaVersion
}
}

Gradle Eclipse not working

My build.gradle script:
apply plugin: 'groovy'
apply plugin: 'eclipse'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile (
'org.apache.camel:camel-core:2.14.1',
'org.apache.camel:camel-mail:2.14.1'
)
}
When I run gradle eclipse I see:
:myapp:eclipseClasspath
:myapp:eclipseJdt
:myapp:eclipseProject
:myapp:eclipse
BUILD SUCCESSFUL
Total time: 3.694 secs
When I run gradle clean build I get a similar BUILD SUCCESSFUL message. But when I refresh my project in Eclipse, I don't see a Referenced Libraries folder with Camel Core or Camel Mail in it, instead under the Problems tab I see 3 problems:
Project 'myapp' is missing required library: 'D:\workspace\myapp\unresolved dependency - org.apache.camel camel-core 2.14.1'
Project 'myapp' is missing required library: 'D:\workspace\myapp\unresolved dependency - org.apache.camel camel-mail 2.14.1'
The project cannot be built until build path errors are resolved
What is going on here? On a perhaps-somewhat-related note, I am on Eclipse Juno, and going into Properties >> Java >> Compiler, I don't seem to have an option to set my Eclipse Java Compiler to 1.8, only 1.7. Perhaps my Eclipse instance is too old to handle Java 8?
If that's your full build script, you're missing repository definitions (where to go get the artifacts). Try adding:
repositories {
mavenCentral()
}
Here's the backing class for repositories {} in case you need to add a custom URL.

Running a jar produce from gradle build

I have a simple gradle file from which I build a jar file.
When I run the jar file however I get and error: 'Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$'
The build.gradle looks like this:
apply plugin: 'scala'
apply plugin: 'distribution'
def mainClass = "com.domain.Hello"
distributions {
custom {}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.4'
}
jar {
manifest {
attributes 'Main-Class': mainClass
}
}
task run(type: JavaExec, description: "Runs the project") {
main = mainClass
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
When you run the jar you need to include the scala-library on the classpath, like this
scala -cp [scala library jar] [project jar]
If you don't want to have to do this, take a look at this blog post http://blogs.steeplesoft.com/posts/2013/09/04/building-fat-jars-with-gradle/
You need to include ''scala-compile' package beside 'scala-library'

Gradle dependencies for non multi project environment

After some research, I was able to create a non multi project with two projects. Sounds strange? Isn't. It's normal that you don't put all your projects under one root project.
Reading a lot about multi project builds I wondered, why it is so complicated to use one project with another that are not in the same root project.
The only way, that worked for me so fare, is to use Maven publishing.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
group = 'foo'
version = '0.2.1'
repositories {
mavenCentral()
mavenLocal()
}
jar { baseName = "${project.group}.${project.name}" }
publishing {
publications {
maven(MavenPublication) {
artifactId "${project.name}"
artifact sourceJar { classifier "sources" }
from components.java
}
}
}
task sourceJar(type: Jar) { from sourceSets.main.allJava }
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
Using: gradlew clean build publishToMavenLocal creates two jar libraries (binary and source) at the correct location in the local maven repository .m2
The second project, that depends on the first one has this build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
group = 'foo2'
version = '0.1'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile("foo:Gradle.test2:0.+")
}
jar { baseName = "${project.group}.${project.name}" }
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
Worked for me. After rebuilding the dependencies I always have the newest version.
Now why do I need the Maven workaround when using Gradle? Can't I publish to the local Gradle cache? What Alternatives are available?
Creating a project dependency without using a common root project is easy once you know how:
In the second project (ProjectTwo), that depends on the first project (ProjectOne), put the following in your settings.gradle file:
include ':ProjectOne'
project(':ProjectOne').projectDir = new File(settingsDir, '../ProjectOne')
The second line above assumes that ProjectOne shares the same parent directory as ProjectTwo, but you can use any relative path to set the correct directory location. Now you can add ProjectOne as a dependency in ProjectTwo's build.gradle file:
dependencies {
compile project(':ProjectOne')
}