include a gradle 'war' project in another project - java

I'm new to Gradle and trying to work out how to get my dependencies working correctly.
I have a project which builds a war using the 'war' plugin. I'd then like to use that project, and some other similar projects, to deploy those wars.
However, I can't work out how to make the project which is responsible for assembling these other projects dependent on the war projects.
The project 'war1' has a single 'war' method, I’ve tried the following
dependencies, none of which work:
dependencies {
war project(path: ':war1')
/* or */ compile project(path: ':war1')
/* or */ assemble project(path: ':war1')
}
Could not find method xxx() for arguments [DefaultProjectDependency{dependencyProject='project ':war1', configuration='default'}]

You can specify dependencies on .war artifacts in Gradle like this example...
dependencies {
runtime "org.jasig.cas:cas-server-webapp:3.5.2#war"
}
NOTE: this example is for an external dependency, not a dependency on another sub-project within the same multi-project setup. Looking at the api docs, it appears that you would do that like this...
dependencies {
runtime project(path: ':war1', configuration: 'war')
}
Also -- perhaps you could benefit from the Gradle WAR overlay plugin. This plugin allows you to "enhance" a war dependency by adding or tweaking a few files within it, and thereby provide some configuration choices that your project knows about, but the original war didn't.

Related

How to import 3rd party library using mavin into Java project using gradle?

I want to use the sxcml-java library in my son's school's robotics code (currently a private repo).
The library uses Maven. I was able to successfully include the library in a test project using Maven.
However, I've just discovered that the existing robotics project code uses Gradle. I don't know either Maven or Gradle, and I haven't programmed in Java in almost 30 years.
How can I most easily use scxml-java - which itself has external 3rd party dependencies — in the robotics project?
This question is similar to this one, but the solution there was easy because both projects were using Gradle.
Provided the package is published in an artifactory, which is the case (See here), you can just include it as any other Gradle dependency (using groupId, artifactId and version), regardless of what build system was used to build it in the first place.
dependencies {
implementation 'com.nosolojava.fsm:scxml-java-implementation:1.0.1'
}
If you use IntelliJ IDEA, pasting the Maven dependency block into the build.gradle file will automatically convert it into the Gradle dependency format like the one above.
Please note however this does not apply to plugins, only to regular dependencies.
If You install your jar or third party jar into maven local repo like ~/.m2
you can add mavenLocal()
repositories {
mavenCentral()
// * Require by Use JAR install to Maven Local Repo your .m2
mavenLocal()
}
then add implementation to dependencies
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
implementation 'yourGroupId:yourArtifactId:yourVersion'
}
Please mapping yourGroupId , yourArtifactId, yourVersion from your pom.xml
If You only download third party jar into foler like /home/yourName/your-libs
you can add configurations
configurations {
sxcml-java-lib
}
then add dependencies
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
//sxcml-java-lib fileTree(dir: "${System.getProperty("user.home")}/libs", include: "*.jar")
sxcml-java-lib fileTree(dir: "/home/yourName/your-libs", include: "*.jar")
}

How can I add an external folder of java code to my gradle project with a relative path?

I have a Gradle Java project that I want to reference other java code that exists in another repo. I am not quite sure how to do this.
My existing build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.2/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
// implementation project(':project1')
// implementation files('../../project1/lib/build/libs/lib.jar')
}
application {
// Define the main class for the application.
mainClass = 'project2.App'
}
What can I add to this file, to have it reference a relative path that points to a folder with java source files in it?
Well, According to the discussion in the comments, here is how to do it.
project1 has its own build.gradle and setting.gradle
go setting.gradle and add the following after cloning the repo inside project1 folder:
include ':project2'
Now, In your project1 in build.gradle file You can add project2 to dependencies section so it will look like this :
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
implementation project(':project2')
}
the final result :
Any time you build project1, project2 will be built as well.
Any update for project2 will be effected immediately to project1 after any build operation.
Might be helpful to read official docs about multi project builds.
If you don't want to clone project2 inside project1, Then build project2 with gradle command line gradle build
This will result in a Jar file created at project2/build/libs/project2-0.0.1-SNAPSHOT.jar
Then you can add it to project1 with a relative path to this Jar like this:
implementation files('../project2/build/libs/project2-0.0.1-SNAPSHOT.jar')

How to specify "pig-0.13.0-h2.jar" dependency in build.gradle?

To specify a Maven dependency in my project, I provide a name, a group id, and a version. This has been enough for every dependency in my project, save one. Pig has multiple jars in the same artifact (not sure if I have the proper nomenclature; I'm still rather new to Maven), but I only need one.
Specifically, I need pig-0.13.0-h2.jar. However, when I provide the dependency
compile "org.apache.pig:pig:0.13.0"
in my build.gradle, only pig-0.13.0.jar, pig-0.13.0-sources.jar, and pig-0.13.0.pom are downloaded. I need the "*-h2.jar", because that's the correct one to work with my version of Hadoop.
Is there a way to tell Gradle (and, generally, Maven or whatever) that my compile dependency requires this exact jar, and that only this one should be included in the classpath?
What you need is to specify the classifier. The following script will do the job:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile "org.apache.pig:pig:0.13.0:h2"
}
task copyDeps(type: Copy) {
from configurations.compile
into 'deps'
}

How to enable provided scope in subproject{} in gradle?

Suppose we have a multi-project build. Eventually the entire project is bundled into a war. We know the web container will provide some utility jars, say, some logging-related jars, so we want to use a provided kind of scope. The war plugin offers the nice providedCompile and providedRuntime configuration. However, we want to use the war plugin only in the subproject that creates the war. So, the problem is, how can we use something like providedCompile and providedRuntime in other sub-projects? Ideally, we want to apply that scope to the logging jars in a subprojects {} closure.
update
My scenario is like below.
In the root build.gradle, I have something like this:
subprojects {
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
compile 'org.slf4j:slf4j-api:1.7.7' // already provided by the servlet container
compile 'javax.servlet:javax.servlet-api:3.1.0' // already provided by the servlet container
runtime 'ch.qos.logback:logback-classic:1.1.2' // already provided by the servlet container
runtime 'ch.qos.logback:logback-core:1.1.2' // already provided by the servlet container
...
These dependencies are used not just by the war sub-project, but they should be excluded in the war. Therefore, I need some mechanism to achieve this.
BTW, I have already worked out a solution.
There are a couple of ways to achieve a provided scope.
Option 1
Create a custom configuration provided. For example:
configurations {
provided
}
sourceSets.test {
runtimeClasspath += configurations.provided
}
The downside of this approach is that IDEs, for instance, IntelliJ IDEA, probably don't understand custom configuration and hence are not able to pull dependencies correctly. This happened to me with IntelliJ IDEA.
Option 2
Use the predefined compile and runtime configurations. Then, in the build.gradle of the war sub-project, filter out the jars of your choice. For example,
war {
Set exclusions = ['slf4j-api-1.7.7.jar', 'javax.servlet-api-3.1.0.jar',
'logback-classic-1.1.2.jar', 'logback-core-1.1.2.jar'] as Set
classpath = classpath.filter { file ->
!exclusions.contains( file.name )
}
}
With gradle 2.12, it is possible to use compileOnly when using java plugin.
https://docs.gradle.org/2.12/release-notes#support-for-declaring-compile-time-only-dependencies-with-java-plugin

How do you configure the default dependencies in a new Gradle project in IntelliJ Idea

After I added some dependencies to build.gradle
compile(
"org.springframework.boot:spring-boot-starter-web:1.1.6.RELEASE",
"org.springframework:spring-core:4.1.0.RELEASE",
"org.springframework:spring-test:4.1.0.RELEASE",
"javax.inject:javax.inject:1",
"org.mockito:mockito-all:1.9.5",
"org.quartz-scheduler:quartz:2.2.1",
"org.apache.commons:commons-lang3:3.3.2",
"com.google.guava:guava:18.0"
)
and refreshing the IntelliJ Gradle plugin, many more dependencies appear in the Gradle libs list inside "External Libs"
I suspect this is nothing to do with the plugin, but rather gradle augmenting my dependencies from somewhere else but I can't work out from where.
Just like in Maven, the dependencies you declared can have further dependencies that are declared by the package supplier (a.k.a transitive dependency management) - those will be added automatically, just like you observed.
More info here.

Categories

Resources