IntelliJ Gradle deletes its own modules unexpectedly - gradle

This is the dialog indicating the modules which are being deleted
but when I import a new project with build.gradle and I do not enable auto import now I have a dialog asking my if I would like to enable auto import, and the modules are no longer deleted automatically.
Here is the build.gradle file
/*------------------------------------------------------------------------------
Gradle latest file.
Build command:
./gradlew deploy
Create Javadocs at ./latest/docs/javadoc/index.html
./gradlew buildJavadocs
Other commands of interest:
./gradlew wrapper --gradle-version 4.7
./gradlew tasks
./gradlew properties
Manual search for dependencies in Gradle repository:
https://bintray.com/bintray/jcenter/io.appium%3Ajava-client
------------------------------------------------------------------------------*/
group 'com.my.group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/'
}
}
}
sourceCompatibility = 1.8
repositories {
jcenter()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile 'junit:junit:4.12'
compile 'org.seleniumhq.selenium:selenium-java:3.11.0'
compile 'io.appium:java-client:6.1.0' // /compile 'io.appium:java-client:6.0.0-BETA5'
//compile 'io.appium:java-client:6.0.0-BETA5' // compile 'io.appium:java-client:6.1.0'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'com.google.guava:guava:24.1-jre'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
}
task copyToLib(type: Copy, dependsOn: build) {
String dst = "$rootDir/jar"
description "Copy library JAR dependencies to $dst"
from configurations.runtime
into "$dst"
}
task copyToOut(type: Copy, dependsOn: build) {
String dst = "$rootDir/out"
description "Copy library JAR output to $dst"
from jar
into "$dst"
}
task deploy {
dependsOn clean
dependsOn build
dependsOn copyToLib
dependsOn copyToOut
}
task buildJavadocs(type: Javadoc) {
exclude 'srcOld/**'
classpath += sourceSets.test.compileClasspath
source += sourceSets.main.allJava
}
edit
When Gradle deletes the modules, it also delete gradle.build and the gradle wrapper. I've already created a brand new project with the default Java template, and copied and pasted my code into it, but the problem persists.

In case of a Gradle projects IDE configures the project structure including the modules that are included in project according your the build.gradle script. E.g. if you have Create separate module per source set option enabled in IDE Gradle settings IDE creates separate module for each Grale source set. It does it when re-importing build.gradle file. If you have auto-import enabled in IDE Gradle configuration it does it automatically.

Given the limited information, I'll give you an ad-hoc fix, just add -----test as a sub-project directory to your root projects settings.gradle using
include 'root-proj-dir:dir1:dir2:...:----test'
where the directory structure is
root-proj-dir
|
|
----dir1
| |
| |
| ----dir2
| |
| |
| ....----test
----settings.gradle
|
----build.gradle
For more info on dealing with multi-module (or multi-project in Gradle terminology ) builds refer this link

Related

How can I clone a repo from GitHub using gradle?

I have a project which depends on other repos. My goal is to write a gradle task that clones the required repos automatically.
The problem I am having is that when I run my task, gradlew fails because it tries to compile the project dependencies before running my task. Here is what I have:
settings.gradle
include ':app'
include 'testRepo'
project(':testRepo').projectDir = new File('../testRepo')
build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'org.ajoberstar:gradle-git:1.5.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
build.gradle(app)
task clone << {
Grgit.clone(dir: file('../testRepo'), uri: "https://github.com/fakeAccount/testRepo.git")
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile project(':testRepo')
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
}
When I do ./gradlew clone I get:
"Cannot evaluate module testRepo : Configuration with name 'default' not found"
This is because it tries to compile the testRepo project before cloning from GitHub. If I take out the compile project(':testRepo') it works fine.
Any ideas as to how to make this work?

How to setup dependencies in a multi module Android Gradle project with interdependencies?

The Question
How do I setup the dependencies of the modules in an Android Gradle project, where the modules have dependencies on each other, such that I can deploy a build of a new snapshot or release version of all modules in one go?
Project Setup
I have a project that consists of 2 modules: lib-core and lib-help, with lib-help depending on lib-core. I want to publish lib-core and lib-help seperately to Maven Central, such that the pom file of lib-help specifies the dependency on lib-core with the correct version.
The project is structured as follows:
/my-project-name
|
|--/lib-core
| |-- build.gradle
| |-- gradle.properties
|
|--/lib-help
| |-- build.gradle
| |-- gradle.properties
|
|-- build.gradle
|-- gradle.properties
|-- settings.gradle
|-- gradle-mvn-push.gradle
The project configuration files are as follows, showing only the relevant parts (by my judgement):
settings.gradle:
include ':lib-core', ':lib-help'
gradle.properties:
GROUP=com.company
VERSION=5.0.0-SNAPSHOT
gradle-mvn-push.gradle:
apply plugin: 'maven'
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
// GROUP is defined in <root>/gradle.properties
pom.groupId = GROUP
// ARTIFACT is defined in <root>/<module>/gradle.properties
pom.artifactId = ARTIFACT
// VERSION is defined in <root>/gradle.properties
pom.version = VERSION
}
}
}
}
// Contains a lot more to fill in other meta-data like project name,
// developer name, github url, javadoc, signing of the artifacts, etc, etc..
lib-core/gradle.properties:
ARTIFACT=my-lib-core
lib-core/build.gradle:
apply plugin: 'com.android.library'
// Contains more code to configure the android stuff, not relevant for this question
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
apply from: rootProject.file('gradle-mvn-push.gradle')
lib-help/gradle.properties:
ARTIFACT=my-lib-help
lib-help/build.gradle:
apply plugin: 'com.android.library'
// Contains more code to configure the android stuff, not relevant for this question
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':lib-core')
}
apply from: rootProject.file('gradle-mvn-push.gradle')
The Problem
When I run gradlew clean :lib-core:uploadArchives :lib-help:uploadArchives, everything compiles nicely and uploads are being pushed to the correct repository. In the Nexus Repository Manager I can see the artifacts for both modules with the correct name, version, signing, etc., but the dependency of lib-help on lib-core in the generated pom.xml is wrong. It uses the project name as groupId, the module name as the artifactId and an unspecified version name:
<dependency>
<groupId>my-project-name</groupId>
<artifactId>lib-core</artifactId>
<version>unspecified</version>
<scope>compile</scope>
</dependency>
Consequently, projects using lib-help as a dependency can't resolve the lib-core dependency. The correct values that should have been in lib-help's pom.xml are groupId=com.company, artifactId=my-lib-core and version=5.0.0-SNAPSHOT.
To fix this, I thought I'd use a maven dependency in lib-help, instead of a module dependency, like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.company:my-lib-core:' + VERSION
}
Naturally, this version does not exist before the uploadArchives command of lib-core has been executed, but gradle wants to resolve the dependencies of all modules before starting execution of any of the given commands. Thus, it can't resolve this dependency and quits with an error while configuring the gradle projects. Now, I can work around this by temporarily setting the dependency of lib-help to project(':lib-core'), then release lib-core, then adjust the dependency of lib-help to 'com.company:my-lib-core:5.0.0-SNAPSHOT', then release lib-help, but that just doesn't seem the right way. It invites human mistakes (what if I accidentally increase the version number between the to uploadArchives commands?) and it takes several manual, strictly ordered steps.
The Question (reprise)
How do I setup the dependencies of the modules in an Android Gradle project, where the modules have dependencies on each other, such that I can deploy a build of a new snapshot or release version of all modules in one go? (e.g., but not necessarily, with the command gradlew clean :lib-core:uploadArchives :lib-help:uploadArchives)
It's possible to keep the local project dependencies (compile project(':core')), if you rewrite the POM before uploading:
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
// ... other config here ...
pom.whenConfigured { pom ->
pom.dependencies.forEach { dep ->
if (dep.getVersion() == "unspecified") {
dep.setGroupId(GROUP)
dep.setVersion(VERSION_NAME)
}
}
}
}
}
}
}
Here's an example of a working project: https://github.com/hannesstruss/WindFish

How to generate a war file based on two subprojects

I have a project which is splitted into two subprojects.
/project
/sub-project-a (backend with JAVA source which is compiled into JAR file)
/sub-project-b (frontend sources which are compiled with grunt via gradle call)
build.gradle
settings.gradle (contains include 'sub-project-a', 'sub-project-b')
My Question is how can I create a War file with sub-projects and external lib dependencies? The following code snipped is my current build.gradle:
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile project(':sub-project-a')
compile project(':sub-project-b')
compile 'com.google.code.gson:gson:2.2.4'
}
task copy(type: Copy) {
from 'sub-project-a/build', 'sub-project-b/build'
into 'build'
}
build.dependsOn clean, copy
war {
archiveName 'project.war'
}
One detail is important. The java context listener (deep inside project code) work with compiled backend as jar file from WEB-INF/lib folder. This means that all class files can't be easily used from WEB-INF/classes folder.
As you can see I played with dependencies and a custom copy task. I'm not sure what is right gradle way. How should I do this?
SOLUTION
Define with war.from methode, where you get your static sources.
gradle docu
from(sourcePaths) -
Specifies source files or directories for a copy. The given paths are
evaluated as per Project.files().
My changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
SOLUTION (for cleanly closing this question) shamefully taken from the question's originator ;-)
Define subproject dependencies with the "war.from" method, where you get your static sources.
gradle documentation excerpt: from(sourcePaths) - Specifies source files or directories
for a copy. The given paths are evaluated as per Project.files().
Ronny's changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}

Gradle. Multi-project with Web. How to include dependent projects as class files into web-inf/classes

I use the Eclipse IDE to develop the following java eclispe projects:
"AuthRESTService" - The development of REST service
"AuthZcommon" - The Utilities, used cross over projects.
"AuthCore" - the core functions.
The needed external libs are placed into simple project "AuthLibs"
The AuthRESTService depends on AuthCore and AuthZcommon.
AuthCore depends on AuthZcommon.
i need to have the WAR file, that contains NOT AuthZcommon.jar and AuthCore.jar
I need to have the WAR file with class files for that projects.
Currently i have the JARs....
===AuthRESTService - settings.gradle==========
includeFlat 'AuthZcommon', 'AuthCore'
===AuthRESTService - gradle.buid==========
apply plugin: 'java'
apply plugin: 'war'
project.webAppDirName = 'WebContent'
def directories = '../AuthLibs/lib'
dependencies {
providedCompile fileTree(dir: directories)
compile project(':AuthZcommon')
compile project(':AuthCore')
war {
description "Create a war file for the web-application release"
classpath fileTree(dir: directories, exclude: ['**/servlet-api*.jar','**/*test-common.jar'])
}
}
==========/AuthCore - settings.gradle==========
includeFlat 'AuthZcommon'
==========/AuthCore - build.gradle=======
apply plugin: 'java'
def directories = '../AuthLibs/lib'
println directories
repositories {
flatDir(dir: directories , name: 'libs directory')
}
dependencies {
compile fileTree(directories)
compile project(':AuthZcommon')
}
==========

Gradle clean<customTask> not working

I have a custom task to do a Websphere EJBDeploy. I have defined the inputs and ouputs, and get successful incremental compilation, but I can't get the automatically generated clean task to work properly.
According to docs, for a custom task named "ejbDeploy" with a defined output, a cleanEjbDeploy task should be automatically generated.
Pattern: clean<TaskName>: Cleans the output files of a task.
So here's my custom task:
task ejbDeploy(dependsOn: 'jar'){
srcFile = file(jar.archivePath)
destDir = new File("build/ejbDeploy")
inputs.file srcFile
outputs.dir destDir
def cp = project.files(
project.sourceSets.main.output.classesDir,
project.sourceSets.main.resources,
project.configurations.runtime
).getAsPath()
doLast{
destDir.mkdirs()
exec{
executable = wasEjbDeploy
workingDir = destDir
args = [
jar.archivePath,
".",
jar.archiveName,
"-cp",
cp
]
}
}
}
Anyone have any ideas as to why the clean rule isn't working?
[Edit]
Here's the full (anonymized) file contents (this has changed since initial question post):
version = '1.0-SNAPSHOT'
group = 'com.company'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath group: 'name.benjaminAbbitt', name: 'WASEjbDeploy', version: '1.0-SNAPSHOT'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'base'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'javax.mail:mail:1.4.5'
compile 'log4j:log4j:1.2.16'
compile files(fileTree(dir: 'lib', includes: ['*.jar']) )
}
task ejbDeploy(type:name.benjaminAbbitt.WASEjbDeploy, dependsOn: 'jar'){
wasEjbDeployPath = wasEjbDeploy
}
Here's the relevant chunk of "$gradle tasks"
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend
on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles the main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles the test classes.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
dependencies - Displays the dependencies of project ':project'.
help - Displays a help message
projects - Displays the sub-projects of project ':project'.
properties - Displays the properties of project ':project'.
tasks - Displays the tasks runnable from project ':project' (some of the
displayed tasks may belong to subprojects).
IDE tasks
---------
cleanEclipse - Cleans all Eclipse files.
eclipse - Generates all Eclipse files.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Other tasks
-----------
ejbDeploy
Rules
-----
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belongin
g to a configuration.
Pattern: clean<TaskName>: Cleans the output files of a task.
To see all tasks and more detail, run with --all.
BUILD SUCCESSFUL
Total time: 3.321 secs
The clean rule is provided by the base plugin. Since many other plugins (e.g. java) already apply the base plugin, you typically don't have to apply the base plugin yourself. But in case:
apply plugin: "base"

Resources