Gradle build using plugins and dependencies from "flatDir" - gradle

How to make a Gradle build if all the required jars to build the project (plugins+dependencies) are present in flatDir?
I have all the required jars in my D drive under D:/path/to/local/directory. Now when I am trying to do a Gradle build, it fails every time for different reasons. Need help in fixing the same (Gradle version 6.3).
Code in my build.gradle:
buildscript {
repositories {
// If you want to use a (flat) filesystem directory as a repository
flatDir {
dirs 'D:/path/to/local/directory'
}
}
}
plugins {
id "jacoco"
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
id "org.sonarqube" version "2.8"
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'wsdl4j:wsdl4j:1.6.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.ws:spring-ws-test'
testImplementation 'org.apache.httpcomponents:httpclient:4.5.9'
testImplementation 'com.h2database:h2:1.4.199'
}
bootWar {
baseName = 'web-service'
version = '1.0.0'
}
jacocoTestReport {
reports {
xml.enabled true
}
}
sonarqube {
properties {
property 'sonar.projectName', 'Sonar-Gradle-Integration'
}
}
Code in my settings.gradle:
pluginManagement {
flatDir {
dirs 'D:/path/to/local/directory'
}
}

I found out how to build a project from flat directory after some tries (Gradle 6.3).
You must have all the dependencies in the flatDir repository (transitive dependencies as well). I kept all the jars in a single directory inside the root folder named "lib" of my project and modified the build.gradle and settings.gradle like below.
build.gradle:
plugins {
id "org.sonarqube"
id "jacoco"
id 'java'
id 'war'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
// If you want to use a (flat) filesystem directory as a repository
flatDir {
dirs 'lib'
}
}
dependencies {
implementation fileTree(dir: 'lib', include: '*.jar')
testImplementation fileTree(dir: 'lib', include: '*.jar')
}
jacocoTestReport {
reports {
xml.enabled true
}
}
settings.gradle
pluginManagement {
buildscript {
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
classpath fileTree(dir: 'lib', include: '*.jar')
}
}
}
rootProject.name = 'gradleproj'

Related

Spring boot 2.3.5 | publish jar to local maven repository using gradle is not working

I'm trying to publish a spring-boot project to local maven repository using Gradle. however, I'm getting below error.
My Gradle file looks like below
plugins {
id 'org.springframework.boot' version '2.3.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'maven'
}
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
mavenLocal()
}
ext {
set('springCloudVersion', "Hoxton.SR9")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
Thanks,
I have changed the script to following and still it not working
below is the gradle tasks, error message and script
D:\workspaces\test\common>gradle clean publishToMavenLocal
> Task :publishMavenJavaPublicationToMavenLocal FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':publishMavenJavaPublicationToMavenLocal'.
> Failed to publish publication 'mavenJava' to repository 'mavenLocal'
> Artifact common-1.0.jar wasn't produced by this build.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
plugins {
id 'org.springframework.boot' version '2.3.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'java-library'
id 'maven-publish'
}
group = 'com.test'
version = '1.0'
ext {
set('springCloudVersion', "Hoxton.SR9")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
repositories {
mavenCentral()
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
repositories {
maven {
def releasesRepoUrl = "$buildDir/repos/releases"
def snapshotsRepoUrl = "$buildDir/repos/snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
}
}
}
javadoc {
if(JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}
however if remove dependencies, it's working fine
this script is working
plugins {
id 'java-library'
id 'maven-publish'
id 'signing'
}
group = 'com.example'
version = '1.0'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
repositories {
maven {
def releasesRepoUrl = "$buildDir/repos/releases"
def snapshotsRepoUrl = "$buildDir/repos/snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
}
}
}
javadoc {
if(JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}
any idea why my script is not working ??
Thanks
I removed the org.springframework.boot plugin and used the below method to add dependencies and it worked. I would appreciate if someone can share if there is a document with this information.
this is the change I made
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
to
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.8.RELEASE'
}
and
plugins {
id 'org.springframework.boot' version '2.3.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'java-library'
id 'maven-publish'
}
to
plugins {
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'java-library'
id 'maven-publish'
}

How to publish an artifact to a remote repository using gradle

I wanted to publish an artifact to a remote repository identified by a url . Here is my build.gradle file. This is a java gradle project with some dependencies. I was able to publish this to a local .m2 repository and make use of this in another project.
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'maven-publish'
}
group = 'com.mob.ci'
version '1.0-SNAPSHOT'
apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
maven{url 'https://proget1..../maven2/Maven/'}
}
jar {
enabled = true
}
bootJar {
enabled = false
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-couchbase'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
repositories {
maven {
url = 'https://proget1..../maven2/Maven/'
}
}
}
Here is the update the build.gradle file, that resolved the issue. Thank you #soung for the tip. I need to specify the credentials for accessing the repository.
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'maven-publish'
}
group = 'com.mob.ci'
version '1.0-SNAPSHOT'
apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
maven{url 'https://proget1..../maven2/Maven/'}
}
jar {
enabled = true
}
bootJar {
enabled = false
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-couchbase'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
repositories {
maven {
url = 'https://proget1..../maven2/Maven/'
credentials {
username = deployRepoUsername
password = deployRepoPassword
}
}
}
}

Unable to get classpath in Gradle

I need to get the classpath for our Java project in the build.gradle file. I have seen answers saying to use sourceSets.main.runtimeClasspath or configurations.runtime.asPath but those don't work.
When I try:
task fortify(type: Exec) {
def classpath = sourceSets.main.runtimeClasspath
}
Running gradle fortify results in Could not get unknown property 'sourceSets' for task ':fortify' of type org.gradle.api.tasks.Exec.
When I try:
task fortify(type: Exec) {
def classpath = configurations.runtime.asPath
}
Running gradle fortify results in Could not get unknown property 'runtime' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
So we actually have 2 build.gradle files. One of them is at the top level of the project and contains few lines:
plugins {
id 'org.sonarqube' version '2.7.1'
id 'jacoco'
}
group = 'com.mycompany.project'
version = '1.0.1'
sonarqube {
properties {
property "sonar.sources", "ci-cd"
}
}
This is the file I added the task to, which didn't work.
Then in the main project folder, there is a subdirectory that contains another build.gradle file, a src folder, and some other things. This build.gradle file contains a lot more:
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'java'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}
group = 'com.mycompany.project.api'
version = '1.0.2'
sourceCompatibility = '11.0.4'
if (project.hasProperty('buildNum')) {
version(project.version + '.' + buildNum)
}
apply from: 'gradle/docker.gradle'
apply from: 'gradle/jlink.gradle'
apply from: '../gradle/artifactory.gradle'
springBoot {
buildInfo()
}
repositories {
maven {
url 'https://artifactory.mycompany.com/artifactory/dept-group-gradle-cache'
credentials {
username project.artifactory_user
password project.artifactory_apikey
}
}
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Greenwich.RELEASE'
}
}
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.3.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-config-client:2.1.0.RELEASE'
implementation 'org.springframework.data:spring-data-jpa'
implementation 'mysql:mysql-connector-java'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-bean-validators:2.9.2'
implementation group: 'org.latencyutils', name: 'LatencyUtils', version: '2.0.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-inline:2.23.4'
testImplementation 'com.h2database:h2'
}
bootJar.dependsOn 'cleanDocker'
copyDockerFiles.dependsOn 'bootJar'
copyDockerFiles.dependsOn 'packageJRE'
copyDockerFiles {
from files(bootJar)
from files(packageJRE.outputs)
into "${project.buildDir}/docker"
}
sonarqube {
properties {
property "sonar.sources", "src/main"
property "sonar.java.binaries", "src/main"
property "sonar.java.libraries", "/usr/java/latest/lib"
}
}
I added the task without (type: Exec) to this build.gradle file, and sourceSets.main.runtimeClasspath.asPath successfully returned the classpath.

Sibling project seems not using it's dependency's build.gradle script

I have multi-module project with a structure like this:
|fullstack
\backend
|build.gradle
\frontend
|build.gradle
|build.gradle
|settings.gradle
Settings.gradle:
rootProject.name = 'fullstack'
include 'backend'
include 'frontend'
backend depends on ktor, so it's build.gradle contains repository:
maven { url "https://dl.bintray.com/kotlin/ktor" }
And backend module builds well.
Then I want to share some common classes between backend and frontend. I could make third module api and make others depend on it. But now I want to avoid third module and just make frontend to depend on backend.
I added the dependency to the frontend's gradle.build:
compile project (':backend')
and tried to build the whole project, but got an error:
Could not resolve all files for configuration
':frontend:compileClasspath'.
Could not find io.ktor:ktor-server-netty:0.9.1. Searched in the following locations:
https://repo.maven.apache.org/maven2/io/ktor/ktor-server-netty/0.9.1/ktor-server-netty-0.9.1.pom
https://repo.maven.apache.org/maven2/io/ktor/ktor-server-netty/0.9.1/ktor-server-netty-0.9.1.jar
Required by:
project :frontend > project :backend
It obviously didn't scan repositories of backend module. Why?
UPDATE
I've added repositories as said in answers. But when I try to import in frontend/main.kt any class from backend I get:
:frontend:compileKotlin2Jse: D:\...\frontend\src\main\kotlin\main.kt: (1, 8): Unresolved reference: com
e: D:\...\frontend\src\main\kotlin\main.kt: (2, 8): Unresolved reference: com
How to make it visible for frontend?
Root build.gradle:
group 'com.norg.parts'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_ver = '1.2.50'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_ver"
}
}
allprojects {
project.ext {
kotlin_version = kotlin_ver
ktor_version = '0.9.1'
}
repositories {
jcenter()
mavenCentral()
maven { url 'https://dl.bintray.com/kotlin/ktor' }
maven { url 'https://dl.bintray.com/kotlin/kotlinx' }
maven { url 'https://mvnrepository.com/artifac/' }
maven { url "https://dl.bintray.com/kotlin/exposed" }
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
manifest {
attributes 'Main-Class': 'com.norg.parts.MainKt'
}
//TODO include jar from frontend!
}
The problem is that there can be 2 dependencies / repositories blocks. The first one is in the buildscript which looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
This is responsible for setting up tooling and plugins. Then when you set up the config of your project itself you can do several things. If you don't have a multi-module project you just dump it into your build.gradle then the file looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
compile "io.ktor:ktor-server-core:$ktor_version"
}
In your case you should use either allprojects which means that all configuration which you put in that block will be applied to all your projects (including root), or subprojects. In the latter case, the block will be applied to only your subprojects (root not included). This looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
subprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
compile "io.ktor:ktor-server-core:$ktor_version"
}
}
Your problem is that you did not add the repository itself to your project config, only your buildscript config.
If you are planning to mix frontend and backend code you can now use Kotlin Multiplatform Projects. I've written about this here.
Response to your edit:
You really need to move all this to a subprojects block:
subprojects {
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
manifest {
attributes 'Main-Class': 'com.norg.parts.MainKt'
}
//TODO include jar from frontend!
}
}
and you also need to add the backend project as a dependency to your frontend project in that project's build.gradle. I'd strongly advise you to read the Gradle Documentation since this is a Gradle issue not a Kotlin issue whatsoever.
Dependencies are transitive, but repositories are not. This is why most projects have a allprojects block in the root build file defining the repositories for all subprojects at the same time.

gradle conflicting transitive dependency not being included

I have a dependency, my-project that uses two dependencies that use different versions of commons-lang3 but when I build my war artifact, commons-lang3 is not included in the artifact. What could be wrong?
My build.gradle looks like:
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'war'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.3.0.RELEASE'
}
}
compileJava {
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
configurations.all {
exclude group: 'commons-logging'
}
repositories {
jcenter()
maven {
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
url "myrepo.com"
}
}
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:1.1.2.RELEASE'
}
}
dependencies {
compile "my.project:my-project:1.0.0-SNAPSHOT"
// Spring Framework
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-web'
compile 'org.springframework:spring-webmvc'
compile 'org.springframework.security:spring-security-config'
compile 'org.springframework.security:spring-security-web'
// Jackson
compile "com.fasterxml.jackson.core:jackson-annotations"
compile "com.fasterxml.jackson.core:jackson-core"
compile "com.fasterxml.jackson.core:jackson-databind"
// Logging
compile 'ch.qos.logback:logback-classic'
compile 'org.slf4j:slf4j-api'
runtime 'org.slf4j:jcl-over-slf4j'
runtime 'org.logback-extensions:logback-ext-loggly:0.1.2'
// Test
testCompile 'junit:junit'
testCompile 'org.mockito:mockito-core'
testCompile 'org.springframework:spring-test'
providedCompile 'javax.servlet:javax.servlet-api'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.web
}
}
repositories {
maven {
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
if(project.version.endsWith('-SNAPSHOT')) {
url "myrepo.com/libs-snapshot-local"
} else {
url "myrepo.com/libs-release-local"
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}
EDIT Dependency diagram of my.project:my-project
my-project is a library project that my co-worker wrote that is dependent on two other library projects that contain commons-lang3. An example diagram would be
my.project:my-project:1.0.0-SNAPSHOT
+---my.project:my-dependency-1:1.0.0
| +---org.apache.commons:commons-lang3:3.4
+---my.project:my-dependency-2:1.0.0
+---org.apache.commons:commons-lang3:3.3.2
I updated the spring gradle dependency-management-plugin to 0.5.2.RELEASE and that fixed my problem.

Resources