Using Gradle multi-project build apply plugin is applicable to master project - gradle

In a multi-project build using Gradle v5.5.1 I am trying to apply the ear plugin only to certain subprojects (as described in using plugins). You can see here that I'm trying to apply it for subprojects ending in EAR:
subprojects { Project proj ->
afterEvaluate {
if(proj.projectDir.name.endsWith('EAR')){
logger.debug "{} looks like an EAR subproject", proj.name
apply plugin: 'ear'
defaultTasks 'ear'
}
}
However, it applies it to my master project instead (output log):
10:53:36.366 [DEBUG] [org.gradle.api.Project] MAG820PAYMENTRECONEAR looks like an EAR subproject
10:53:36.367 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Apply plugin org.gradle.ear to root project 'master'' started
Then when it gets to the execution of my subproject it says the ear task is not found:
10:53:36.375 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Task 'ear' not found in project ':MAG820PAYMENTRECON:MAG820PAYMENTRECONEAR'.
I know this works because I have other projects doing similar but I can't figure out why this one is working this way.
What am I doing wrong??

Simply use proj.apply plugin: 'ear' to call the method on the sub-project

Related

gradle Could not get unknown property 'war' for project '<project name>' of type org.gradle.api.Project

I have a gradle build with Java sub-projects and had a jacoco task defined in the root build.gradle which I'm moving to the sub-projects that only use it.
By doing that though I am hitting an issue in the ear deploymentDescriptor task where and there are multiple webModule definitions.
The error I'm seeing in the IDE and gradle command line is:
A problem occurred evaluating project ':deployear'.
> Could not get unknown property 'war' for project ':sub-project-two' of type org.gradle.api.Project.
My ear build.gradle contains
apply plugin: 'ear'
ear {
archiveName = 'web-application.ear'
appDirName 'src/main/application'
libDirName 'APP-INF/lib'
deploymentDescriptor {
fileName = "application.xml"
version = "1"
description = "My Web Application"
initializeInOrder = true
displayName = "Web Application"
webModule(project(":sub-project-one").war.archiveName, "context-root/one")
webModule(project(":sub-project-two").war.archiveName, "context-root/two")
}
}
If I comment out webModule(project(":sub-project-two").war.archiveName, "context-root/two") then the project builds successfully.
Both sub-project-one and sub-project-one have java and war plugins applied.
Is there a reason why moving a jacoco dependency from a parent build.gradle to some sub-project build.gradle files would generate this error?
Found a work around.
In my deployear sub-project I added
apply plugin: 'ear'
evaluationDependsOn(':sub-project-two')
and that forced the deployear sub-project to evaluate the sub-project-two project prior to running the ear task.
I thought the same could be done with changing the ordering in the parent settings.gradle but that didn't work for me.

Gradle tasks shown in intellij but can not be run

I have a multi module project with the following structure:
root_project
module1
module2
module3
I try to apply the java plug-in to all projects using the following code:
allprojects {
apply plugin: 'java'
group = 'com.mysoftware'
sourceCompatibility = 1.8
version = '1.3'
repositories {
mavenCentral()
}
}
Additionally I add the javafx plugin to module3. The java and javafx tasks are now shown in the intellij gradle view, but when trying to execute them, I get this error:
Task 'jfxJar' not found in root project 'module3'.
Furthermore, running the tasks task show me that neither the java tasks nor the javafx tasks are available, despite being shown in the gradle view in intellij.
I tried rebuilding and refreshing the whole project without success. I use the Use default gradle wrapper configuration.
The error message you got Task 'jfxJar' not found in root project 'module3' indicates that Gradle considers the subproject module3 as a Root project: this can happen if you created a settings.gradle file in the sub-project directory, which is not a valid setup (only one settings.gradle file can exist in a multiproject build, located in the root directory)

Run my task before a plugin's task?

We're using a gradle file to build a Java WAR file. I know very little about gradle. At the top of build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
We run the gradle with gradle clean install. I'm not sure where these tasks are defined but I assume they're in one of the plugins (I'd guess war).
When I run gradle clean install it seems to print the tasks that it are run:
:clean
:compileJava
:processResources
:classes
:war
:install
Correct me if I'm wrong, but it seems that the task install dependsOn compileJava, processResources, classes, and war.
I need a task I've written to run sometime after clean but sometime before war. Preferably without modifying the plugin.
I've tried indicating that my task mustRunAfter processResources but it doesn't work that way.
How can I inject my task as a dependency on install before the dependency war?
You can declare task dependencies explicitly.
Add following code to your build.gradle file
tasks.war.dependsOn("yourTaskNameHere")
tasks["yourTaskNameHere"].dependsOn("clean")

Invalid classpath publish/ export dependency /ouat-contract. Project entries not supported

I am trying create an Gradle multi project similar to this structure
ouat-services
- ouat-contract
- ouat-servicesImpl (web project)
I followed the eclipse example and define my ouat-services settings.gradle as
include "ouat-contract", "ouat-servicesImpl"
In my ouat-servicesImpl build-gradle I define
dependencies {
compile project(':ouat-contract')
}
My problem starts when I try apply war plug-in in ouat-servicesImpl, I receive the following message in eclipse problem view:
Invalid classpath publish/ export dependency /ouat-contract. Project entries not supported
My ouat-services build.gradle
configure(subprojects) {
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'eclipse'
apply plugin: 'java'
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
def defaultEncoding = 'UTF-8'
[compileJava, compileTestJava]*.options*.encoding = defaultEncoding
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
jar {
manifest.attributes provider: 'Company'
}
}
configure(project(':ouat-servicesImpl')) {
apply plugin: 'checkstyle'
apply plugin: 'eclipse-wtp'
apply plugin: 'findbugs'
apply plugin: 'jacoco'
//apply plugin: 'jetty'
apply plugin: 'pmd'
apply plugin: 'war'
}
buildscript {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.10.1'
}
}
My ouat-servicesImpl build gradle was changed to:
dependencies {
compile project(':ouat-contract')
cxfArtifacts.each { artifact ->
compile "org.apache.cxf:$artifact:$cxfVersion"
}
springArtifacts.each { artifact ->
compile "org.springframework:$artifact:$springVersion"
}
testCompile "org.testng:testng:$testNGVersion"
testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
testCompile "org.springframework:spring-test:$springVersion"
//WAR PLUGIN
providedCompile "javax.servlet:javax.servlet-api:$servletAPIVersion"
runtime "javax.servlet:jstl:$jstlVersion"
}
Is this an eclipse plug-in problem or I am doing something wrong?
Here's the magic steps I've discovered to make it work without messing with Project settings manually.
Run command: gradle cleanEclipse eclipse
as a result of this command Eclipse forgets that the project was supposed to have a gradle nature.
Add gradle nature back to the project by doing Configure -> Convert to Gradle Project.
as a result of this command the error reappears.
if incompatible plugin java version error appears then just delete .settings directory and refresh.
Run command: gradle cleanEclipseClasspath eclipseClasspath
this final step should get it fixed until the next time.
In my case, this was due to mixing "faceted" and non-faceted projects. The projects with the error had been converted to faceted form, and the project they referenced which it was complaining about had not been. You can configure the project to be faceted via use of the eclipse-wtp plugin, by adding this to your ouat-contract gradle file:
eclipse{
wtp{
facet{}
}
}
This will add facets for Java and a utility module when using the java and war plugins (see the EclipseWTPFacet documentation for more information on the defaults and manually adding facets if you aren't using the war plug-in). The utility module part is the key to avoid the error.
Note that within this block you can also access the facet file directly to perform manual XML manipulation if you need to do other things, like specify a particular Apache Tomcat Runtime or or similar
Once you make this change, you can use Eclipse to do Gradle -> Refresh All on ouat-contract within your workspace - once I did this, the error went away
I've also run into this problem long time ago. It really seems to be the problem related to the Eclipse plugin included in "Gradle IDE Pack" (as it works from the command line without problems).
My setup is probably way more complex than Yours (I'm including modules from one top-level gradle project into another top-level gradle project), but to overcome this specific error
Invalid classpath publish/ export dependency /my-project. Project entries not supported
... i excluded project dependency if some specific gradle property was missing:
if(project.hasProperty("myProjectRefAddedFromIDE")) {
println "NB! Build script started with property 'myProjectRefAddedFromIDE' - expecting that this project in IDE is configured to add the direct reference to my-project"
} else {
compile project(':my-project')
}
And to add the property "myProjectRefAddedFromIDE" only from IDE, i have configured eclipse plugin as follows:
Window -> Preferences -> Gradle -> Arguments -> Program arguments -> Use: ´-PmyProjectRefAddedFromIDE´
Just a warning: this will probably work for you, but there might be some other problem with Your setup, as for simple multi-module project (that doesn't include modules form another multi-module project) I don't have to use this workaround.
This works for me to remove the duplicate jar files from JRE System Library.
Steps Right click on Project and go to Build Path->configure build path->Libraries.
Remove the jars that are not in the classpath or duplicated in Maven dependency.

Why is uploadArchives not listed at the tasks list?

i thought uploadArchives is a task which is provided by the java plugin.
In my build.gradle i use the java plugin:
apply plugin: 'java'
But if i invoke gradle tasks on command line, i can't see the uploadArchives task.
Even not with gradle gradle tasks --all
The uploadArchives task is listed in the gradle java plugin documentation
see http://www.gradle.org/java_plugin (table 11).
I use gradle version 1.0-milestone-6.
I can invoke gradle uploadArchives without error, but the task is not listed.
The uploadArchives task is added as a Rule to your build script and not explicitly by name. In the output of "gradle tasks" you should see this line:
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
This means, that for each configuration in your build file, an according uploadTask exist. The java plugin adds an configuration named archives to your build script. By adding the configuration "archives" to your build script explicitly by the java plugin, the uploadArchives task is added implicitly too.
There are scenarios, where gradle can't know what tasks need to be materialized by a rule.
E.g.
tasks.addRule("Pattern: ping<ID>") { String taskName ->
if (taskName.startsWith("ping")) {
task(taskName) << {
println "Pinging: " + (taskName - 'ping')
}
}
}
There is no way to figure out which ping tasks should be shown as they are just materialized when triggered from commandline via 'gradle pingServer1 pingServer2 pingServer3'
regards,
René
The uploadArchives task is a part of the maven-plugin. You have to add:
apply plugin: 'maven'

Resources