How to configure the hamcrest dependency for gradle? - gradle

Again an abslout beginer question :-(
My gradle version is: Gradle 6.3
I initilized a small gradle project using gradle init for java and junit5
to learn junit5 and jmockit :-)
I tried to add some tutorial classes but gradle cannot resolve the hamcrest dependency :-(
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest
testImplementation 'org.hamcrest:hamcrest:2.2'
as well as
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
testImplementation 'org.hamcrest:hamcrest-all:1.3'
on the hamcrest web site, this hint is given
http://hamcrest.org/JavaHamcrest/distributables#using-hamcrest-in-a-gradle-project
here my build.gradle file:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.3/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
google()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
testImplementation 'org.hamcrest:hamcrest:2.2'
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
}
application {
// Define the main class for the application.
mainClassName = 'jmockit_examples.App'
}
test {
// Use junit platform for unit tests
useJUnitPlatform()
}

Related

How to change name of java library when build with gradle?

I'm trying to build a java library for my other java projects. I'm also trying to learn gradle. There is a tutorial : https://docs.gradle.org/current/samples/sample_building_java_libraries.html shows how to build libraries with gradle.
But somehow when I use gradlew build it always gives me lib-< version >.jar and creates a folder called lib and I can't change it.
This is my settings.gradle
rootProject.name = 'myOwnLibrary'
include('lib')
this is my build.gradle (inside lib folder)
plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
}
version = "0.1.1"
tasks.named('jar') {
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
}
repositories {
// Use JCenter for resolving dependencies.
jcenter()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'
// This dependency is exported to consumers, that is to say, found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
}
With Kotlin DSL example, you can add in your tasks jar the following snippet:
tasks.jar {
manifest {
attributes(mapOf("Implementation-Title" to rootProject.name,
"Implementation-Version" to project.version))
}
archiveBaseName.set(rootProject.name)
}
where rootProject.name, is the value localized into settings.gradle.kts file.

Don't know how to include joinfaces into a multi module gradle build

An old application of mine is a Java 8 Enterprise mololith and i want to migrate it.
I want to migrate it into a spring boot project including primefaces,
myfaces and omnifaces by using joinfaces and split it into smaller parts (multi-modules-project)
for better maintainance.
So i split the project into the following modules to became an working prototype:
Short description of the modules:
Common / ComDb : Here are only generated JPA entities / classed directly from the database which classes can be used by all modules
Libraries / LibPrimeFaces : Here are only the latest primefaces elite JAR's which should be used in SpringBoot
Libraries / LibPrimeFacesTheme : Here are a buyed primefaces theme which will be bundeled and included by a generated JAR.
Services / * : Each spring boot service which should be used is located here, so it can be used in the main application or from an external service over REST
Application : Here is the spring boot application which should include all of the things above - even a runnable jar-file at last
The problem now for me is the usage of gradle which is completly new for me:
I dont't know how to integrate joinfaces in combination with my buyed primefaces-8.0.5.jar from elite subscription, omnifaces 3 and myfaces by using gradle.
The most manuals are for maven, but it seems that they are not working for gradle if i
convert the scripts.
Currently the whole project compiles and start without any errors but now i am at the point where i can't find a working example of using joinfaces and gradle in a multi module environment like mine.
Here are the main gradle-scripts:
The root-scripts:
gradle.properties (Holding the versions):
VersionSpringBoot=2.3.5.RELEASE
VersionSpringDependencyManagement=1.0.10.RELEASE
VersionPrimeFaces=8.0.5
settings.gradle (includes and names):
rootProject.name = 'EcoCalcDD4Web'
include 'Common:ComDb'
include 'Libraries:LibPrimeFaces'
include 'Services:ServiceConfigWebManagement'
include 'Application'
build.gradle (Main build file):
buildscript {
repositories {
mavenCentral()
maven {
url './Libraries/LibPrimeFaces'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${VersionSpringBoot}")
}
}
allprojects {
group = 'com.skf.rocket'
version = '1.0.0'
repositories {
mavenCentral()
maven {
url './Libraries/LibPrimeFaces'
}
}
}
subprojects {
apply plugin: 'java-library'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
maven {
url './Libraries/LibPrimeFaces'
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${VersionSpringBoot}")
}
}
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
}
And here the application build.gradle - file where JoinFaces should be used with my primefaces, omnifaces and myfaces.
Here i don't know what to add to get all started with joinfaces:
apply plugin: 'org.springframework.boot'
bootJar {
mainClassName = "com.skf.rocket.EcoCalcDd4WebRocket"
}
// Maven dependencies
dependencies {
// Internal dependencies
api project(':Common:ComDb')
// Implementation project('Libraries:LibPrimeFacesTheme')
api project(':Services:ServiceConfigWebManagement')
// External dependencies
// JoinFaces + MyFaces + PrimeFaces - TODO ??
// Development tools with HotReDeploy
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// Automatically configuration
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
// Lombok - Utility
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// Global cache
implementation 'org.springframework.boot:spring-boot-starter-cache'
// Data access + MS SQL driver
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
// Data validation
implementation 'org.springframework.boot:spring-boot-starter-validation'
// Web + RestService
implementation 'org.springframework.boot:spring-boot-starter-web'
// Session management
implementation 'org.springframework.session:spring-session-core'
// Boot Acturator - Application monitoring and alive checks
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
// Unit test
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
At first, you should have a look at these resources:
https://docs.gradle.org/current/userguide/intro_multi_project_builds.html
https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver
https://docs.joinfaces.org/4.3.4/reference/#_gradle
https://docs.joinfaces.org/4.3.4/reference/#_example_using_myfaces_instead_of_mojarra
https://github.com/joinfaces/joinfaces-gradle-jar-example
To get started with JoinFaces, you need a dependency on org.joinfaces:jsf-spring-boot-starter.
Because you want to use MyFaces instead of Mojarra, you have to exclude it and pull MyFaces instead.
I'd start with these dependencies:
implementation ("org.joinfaces:jsf-spring-boot-starter") {
exclude module: "mojarra-spring-boot-starter"
}
implementation "org.joinfaces:myfaces-spring-boot-starter"
implementation "org.joinfaces:omnifaces1-spring-boot-starter"

How to use spring boot in gradle without the spring boot gradle plugin

Can anyone show me or point me to a spring boot gradle project that does not make use of the spring boot gradle plugin.
I'm looking for something like a spring boot starter web hello world example that doesn't use the gradle plugin.
I can't imagine that the plugin is a requirement, but a search for examples all seem to lean on the gradle plugin, which lets just say is not an option in my environment, and no I can't switch to maven either.
Ideally the gradle build would work by adding something like the following:
gradle.properties
springBootVersion=2.1.3.RELEASE
build.gradle
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
}
I used the spring dependency management plugin, and it works
buildscript {
ext {
springDepManagementVersion = '1.0.10.RELEASE'
springBootVersion = '2.6.6'
springCloudVersion = "2021.0.1"
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:${springDepManagementVersion}"
}
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
}
}
dependencies {
implementation "org.springframework.cloud:spring-cloud-starter-sleuth"
implementation 'org.springframework.boot:spring-boot-starter-json'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
...
}
I can't use spring boot gradle plugin, since I can only use gradle 6.7.1, while spring boot gradle plugin requires gradle version at least 6.8 to support spring boot 2.6. I was inspired by the spring cloud bom solution.

How are some gradle dependencies working with no version supplied

As far as I know gradle requires a version number when setting dependencies, but partial wildcards are allowed. For example if I want Guava, I cannot do this as it fails:
compile('com.google.guava:guava')
It has to be (as an example):
compile('com.google.guava:guava:21.0')
However, I'm learning Spring, which has the following:
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")
How are these dependencies working with no version supplied?
Is it because of the following, but I thought these lines were required only for my plugin 'org.springframework.boot':
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
It is worth mentioning that the trick is called BOM (bill of materials) and the actual versions can be checked in the related POM file (in this example, it is for the version 2.7.0) inside spring-boot-dependencies package. This is mentioned in the Spring Boot official documentation here: Build Systems.
Another way that Spring provides this (for non Boot projects) is through Spring Platform BOM where it actually provides version for the following dependencies.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'
}
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:Athens-SR2'
}
}
TL;DR - spring boot uses custom dependencies resolver.
A spring boot plugin that is applied with the following piece of code:
apply plugin: 'spring-boot'
handles the dependencies that are listed without version. This logic is implemented in this class which delegates it to here. DependencyManagementPluginFeatures are applied here.
The spring boot gradle plugin documentation states the following:
The version of the spring-boot gradle plugin that you declare
determines the version of the spring-boot-starter-parent bom that is
imported (this ensures that builds are always repeatable). You should
always set the version of the spring-boot gradle plugin to the actual
Spring Boot version that you wish to use.
Spring Boot Dependency Management Plugin is not necessary.
You may use build-in Gradle BOM support instead of Spring Boot Dependency Management Plugin
For example:
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.0.RELEASE'
}
repositories {
jcenter()
}
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
}
and for multi-module project:
in root build.gradle :
plugins {
id 'java-library'
id 'org.springframework.boot' version '2.1.0.RELEASE'
}
allprojects {
apply plugin: 'java-library'
repositories {
jcenter()
}
}
dependencies {
implementation project(':core')
implementation 'org.springframework.boot:spring-boot-starter-web'
}
and in core/build.gradle
dependencies {
api platform('org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Gradle: disable autodownloading dependencies

I created a Gradle project in Eclipse, and the following build.gradle was created.
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.18'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
Afterwards, the slf4j module was downloaded and cached in ~/.gradle.
But I didn't want it!! My Linux distribution contains slf4j in the repositories, so I'd rather use pacman/apt-get/etc. to install the needed module.
I'd rather Gradle errored out that the module is missing, or download the needed modules only if a flag is specified.
How can I stop Gradle from being overzealous?

Resources