how to exclude test dependences in gradle - java

first, I have a project like this:
project-a
src
main
java
A.java
test
java
ATest.java
then, I have another project like this:
project-b
src
main
java
B.java
test
java
BTest.java
the build.gradle configuration, project-b dependence project-a
dependencies{
compile project(":project-a")
}
the question is BTest.java can access ATest.java, how to avoid this?
-------------------show more detail---------------
settings.gradle
rootProject.name = 'test-dependence'
include 'project-a', 'project-b'
project-b/build.gradle
dependencies {
compile project(":project-a")
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Unfortunately there's a bit of an impedence mismatch between Gradle modules and IntelliJ modules since Gradle allows multiple classpaths (configurations) in a module and IntelliJ has a single classpath per module.
Basically IntelliJ will allow BTest.java to access ATest.java but if you built from command line, Gradle won't allow it.
Try the following in intellij Gradle Settings.
Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle: check create separate modules per source set
Related question here

Related

Gradle dependencies: package does not exist

I know this has been asked multiple times but the questions have multiple answers.
I'm trying to use a Java package that's a dependency of my dependency. Let's say I've built this gradle project called "ee_tools". And that has a dependency called "my_models", which has a package called "com.mycompany.my_models.db". So in the ee_tools project build.gradle, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "my_models", version: "1.0.0"
}
Then in my current project, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "ee_tools", version: "1.0.0"
Shouldn't this mean that the public classes in my_models are accessible through ee_tools to my current project? Gradle was able to find both in my artifactory instance. And the gradle dependencies command shows ee_tools under the compileClasspath, implementation, and testCompileClasspath trees, but not with its children dependencies. It also shows up in the runtimeClasspath and testRuntimeClasspath trees with its children dependencies, including my_models.
I am also able to see that package inside the jar on the left side of IntelliJ, under the "External Libraries" tree, along with some classes.
But when I try to use the my_models package in my current project, IntelliJ can't find it and it fails a gradle build with the error
error: package com.company.my_models.db does not exist
It can't find any package in that dependency. What gives? Am I declaring the dependencies incorrectly? Or is this a gap between the gradle binary in my command line vs IntellJ and gradlew?
If ee_tools depends on my_models, then your gradle file in ee_tools should be like
implementation project(path: ":path:to:my_models", configuration: 'default')
:path:to:my_models is defined in settings.gradle in project root path like this:
include ':path:to:my_models'

Why gradle removes dependency when I build a project

I have no idea why gradle is doing this to me. I have a multiproject that looks like this:
|Parent
|client
|common
|service
Parent is just an empty project and client, common and service are gradle java projects:
I have some classes that are used in both client and service, therefore I wanted to create a common project and build a jar that I would later use in both service and client. I can build the common jar, but whenever i try to do 'add dependency on common' and then try to 'refresh gradle', it removes the dependency and fails to build!
This is what I do:
Then I press this because I want to build it:
And it just removes the dependency!!!
This is build.gradle file from client project:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'sot.rest'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework', name: 'spring-web', version: '5.2.4.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}
Please help, Im desperate!!
Check out my answer on your previous question. It should give you an idea how to structure and declare the multi-module gradle projects.
I think that when you add the dependency on module with IntelliJ, it just adds it to the project structure through project settings in IntelliJ. And later, when you hit refresh, IntelliJ configures the project based on Gradle files.
To make it working, the Parent project should also be a Gradle project. If it isn't just add build.gradle and settings.gradle under the Parent directory.
Then in settings.gradle add the subprojects like:
rootProject.name = 'Parend'
include('common') //this adds the module
include('client')
include('service')
And later, in build.gradle files of client and service modules add the common module as dependency with:
dependencies {
implementation project(':common')
//...
}
If you are going to work more with Gradle, you could take a look at this article about overall insight on Gradle.
Edit:
(I understood that when you use implementation it doesn't throw errors)
To work with multimodule project with Gradle, the root project also needs to be a Gradle project. The root might or might not contain any source code, but it needs to have its own Gradle files.
So if your project structure needs to be like:
Root project 'parent'
+--- Project ':client'
+--- Project ':common'
\--- Project ':service'
Then the parent and submodule projects need to be set as Gradle projects.
To make it the parent project needs to have at least a settings.gradle file and declared includes for submodules, similarly to:
rootProject.name = 'parent'
include 'common'
include 'client'
include 'service'
Each of modules (client,common,service) must have a build.gradle files under its directory. Submodules using common, so the service and client must add the common as dependency in their own build.gradle files, like:
dependencies {
implementation project(':common')
//and rest of required dependencies
//testCompile group: 'junit', name: 'junit', version: '4.12'
}
Then you should be able to import public classes from common in those submodules and rebuild or reimport project without error.
Since the parent project doesn't contain any source code, then it doesn't need its own build script, but then build file of all of the submodules needs to declare the java plugin on top of the build file:
plugins {
id 'java'
}
Since you are working with IntelliJ and your project could have different structure previously, then the project structure in IntelliJ setting could be messed up now.
To fix it you could go to File->Project Structure->Modules and remove/reimport the parent module again.
If you don't have many classes now, I'd recommend you to create a new project. In the "New Project" window pick Gradle and uncheck Java in "Additional Libraries and Frameworks". This will create blank Gradle project.
After the project is generated, do a right mouse click on parent, and select New->Module. In new module window again pick Gradle and leave the Java checked (since the submodules will contain source code).
With that, the IntelliJ will automatically include created module to the settings.gradle file of root/parent project and the build file of that module will contain the basic configuration (e.g. the java plugin).
But you still add the dependency of one module in another in the build.gradle file of that module.

Vaadin 12 + Gradle 5 Create executable Fat jar

I need to use Jetty and Vaadin and build a fat jar.
My workspace is based around Gradle 5, Its a gradle multi module project
Parent
Common-Lib
Core-Lib
Jetty+Vaadin
I followed the documentation which i found here:
https://vaadin.com/tutorials/embedded-jetty-server-in-vaadin-flow
The documentation explains how to create Jetty's WebAppContext and start Jetty Server instance all based around maven.
Expect as i said my workspace is based around gradle, so instead of copying the pom.xml i rewrote it to the gradle build script which looks as follows:
plugins {
id 'idea'
}
group = 'some.example.jetty.and.vaadin.fatjar'
version = '1.0.0'
dependencies {
implementation project(':Core-Lib')
implementation project(':Common-Lib')
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compile group: 'org.eclipse.jetty', name: 'jetty-continuation', version: '9.4.14.v20181114'
compile group: 'org.eclipse.jetty.websocket', name: 'websocket-server', version: '9.4.14.v20181114'
compile group: 'org.eclipse.jetty.websocket', name: 'javax-websocket-server-impl', version: '9.4.14.v20181114'
compile group: 'com.vaadin', name: 'vaadin-core', version: '12.0.7'
}
My problem is that during the build gradle outputs a .war file. I cannot use a .war file.
This project is supposed to be a plug-in module for another application, which i do not have sources for. The application just loads a jar files from specific folder, only jar extension is supported.
My question is: How can i create standard "unshaded" uber jar/fat jar instead of .war
With the word "unshaded" i want to unpack all JAR dependencies, and repack them into the final JAR.

Gradle compileTestJava fails during gradle clean build

I'm currently having this strange issue with gradle build. Below are the details.
I currently have a java-spring boot based multi module gradle project in the following structure
RootProjectDir
SubProjectA
SubProjectB
SubProjectCommon
The build.gradle file of each one of projects is as below
RootProjectDir build.gradle
dependencies {
compile project(":SubProjectA")
compile project(":SubProjectB")
compile project(":SubProjectCommon")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
SubProjectA build.gradle
dependencies {
compile project(":SubProjectCommon")
}
SubProjectB build.gradle
dependencies {
compile project(":SubProjectCommon")
}
SubProjectCommon build.gradle
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-web')
.....
.....
}
When I execute the
gradle clean build
the build is failing during the compileTestJava phase of SubProjectA. SubProjectA tests have compile time dependency on classes in SubProjectCommon.
If I just execute the following
gradle :subProjectA compileTestJava
the build is successful again.
It is failing with the message that SubProjectCommon classes could not be resolved.
The strange thing is that in the IntelliJ IDEA it doesn't show any compilation issues for the SubProjectA test classes and test executes fine. Also when I just execute the
gradle clean test
everything works fine.
I even tried putting a testCompile dependency on SubProjectCommon in the SubProjectA build.gradle like this
SubProjectA build.gradle
dependencies {
compile project(":SubProjectCommon")
testCompile project(":SubProjectCommon")
}
but still doesn't work
PS:-I currently have written test cases only for SubProjectA classes.
IDEs do not honor module paths very nicely, especially Eclipse, so everything is usually included together, thus you do not get any path problems.
Gradle makes clean distinctions between different projects.
So if your classes were in the test folder, You may need to reference the test sets properly using the below:
testCompile project(":SubProjectCommon").sourceSets.test.output
or
compile project(":SubProjectCommon").sourceSets.test.output
depending on which sourceSet is using classes from the other project.

Gradle add library to classpath

I am using Gradle for a project. My build.gradle has got JUnit as a dependency, but every time I import the project again it does not recognize the JUnit library and asks me to add it to classpath. This problem does not only apply to JUnit, but also to other dependencies. Did I add JUnit to the wrong place?
PS: The project builds just fine, it's just the IDE (IntelliJ) that's marking everything red.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The dependency declaration is correct. Probably you're opening the project in IntelliJ IDEA the wrong way.
I suggest you to open a Gradle project this way:
Click the File | Open menu
Select the build.gradle file
Here is further information about importing Gradle projects (see also the side note in that page).
download the external jar file from mvn repository and add it to libs folder.
Gradle-project(your project)->libs(inside libs folder paste jar file). if you don't have libs folder just create it.
go to build.gradle
dependencies { compile files('libs/your_jar.jar') }
reload the gradle project.
it's worked for me. i hope it's work for you guys also...

Categories