Configure gradle projects in SonarQube - gradle

I am working with Liferay DXP and I would like integrate SonarQube in my workspace, I am using gradle.
My workspace is called: test-workpace
My gradle.properies file (path: test-workspace/gradle.properties) is:
systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.sourceEncoding=UTF-8
systemProp.sonar.forceAuthentication=true
systemProp.sonar.login=<mytoken>
# Definición de variables para el proyecto.
description = 'Gradle - Sample Project'
group = 'com.test.sonarqube.gradle'
version = '1.0.0'
My build.gradle file (path: test-workspace/build.gradle) is:
buildscript {
repositories {
mavenLocal()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath group: "org.sonarsource.scanner.gradle", name:"sonarqube-gradle-plugin", version:"2.5"
}
}
group = 'com.test.sonarqube.gradle'
apply plugin: "org.sonarqube"
When I execute "gradle sonarqube" all workspace is scanned but I would like to configure each modules like a project in SonarQube.
Someone know how to configure gradle files to do it?.
Thank you very much!

As Olaf points out: This question was also posted to https://web.liferay.com/community/forums/-/message_boards/message/104477676
You could configure all the subprojects as a different SonarQube project with the following in your build.gradle:
subprojects{
sonarqube {
properties {
property 'sonar.projectName', "${-> project.name}"
}
}
}
You can also set the property sonar.projectKey or any other property from https://docs.sonarqube.org/display/SONAR/Analysis+Parameters
I took the idea of the lazily evaluated project name from: How can I make Gradle extensions lazily evaluate properties that are set dynamically by tasks?

Related

Trouble consuming Gradle plugin project with 3rd party maven repository

I am attempting to create a Gradle plugin that is set up like this:
plugins {
id 'java-gradle-plugin'
id 'groovy'
}
repositories {
mavenCentral()
jcenter()
// note the custom maven repo
maven { url = "https://maven.gentics.com/maven2" }
}
dependencies {
implementation gradleApi(),
localGroovy(),
"com.gentics.mesh:mesh-rest-client:${MESH_VER}"
testImplementation gradleTestKit()
testImplementation("org.spockframework:spock-core:${SPOCK_VER}") {
exclude module : 'groovy-all'
}
}
// the rest...
However if I try to consume this plugin in another project with includeBuild "./gradle-mesh" in the settings.gradle, I get the following error:
A problem occurred configuring root project 'functionalTest'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not find com.gentics.mesh:mesh-rest-client:1.7.1.
Searched in the following locations:
- https://plugins.gradle.org/m2/com/gentics/mesh/mesh-rest-client/1.7.1/mesh-rest-client-1.7.1.pom
If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
Required by:
project : > project :gradle-mesh
However, I am able to execute the functionalTest in the actual plugin tests without issue. Getting rid of that line in the settings.gradle and renaming the folder to buildSrc works as well.
Is there a way to configure the buildscript dependencies from within the plugin entry point? I tried the following, but it didn't make any difference.
class GradleMeshPlugin implements Plugin<Project> {
public void apply(Project project) {
project.buildscript.repositories.configure {
mavenLocal()
jcenter()
maven { url = "https://maven.gentics.com/maven2" }
}
// dependency is referenced here...
}
}
Any ideas how to go about this?
Full Project: https://github.com/wawebio/gradle-mesh
System:
------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------
Build time: 2020-08-25 16:29:12 UTC
Revision: f2d1fb54a951d8b11d25748e4711bec8d128d7e3
Kotlin: 1.3.72
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM: 11.0.8 (Ubuntu 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OS: Linux 5.4.0-47-generic amd64
Update
I was also able to get it to work by adding the following in the consuming project's settings.gradle
pluginManagement {
repositories {
mavenCentral()
jcenter()
maven { url = "https://maven.gentics.com/maven2" }
}
}
rootProject.name = 'website'
includeBuild "./gradle-mesh"
Is there a way to do this from within the plugin application itself?
You need to specify your mvn package‌‌‌​​‌​‌‌​‌‌‌‌‌‌​​​‌​‌‌​‌‌‌‌ differently, which is the first one.
<configuring-project>

Gradle apply plugin vs plugins

I have a simple plugin with greet task doing some 'Hello World' print.
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
}
group = 'standalone.plugin2.greeting'
version = '1.0'
gradlePlugin {
plugins {
greeting {
id = 'standalone.plugin2.greeting'
implementationClass = 'standalone.plugin2.StandalonePlugin2Plugin'
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'standalone.plugin2.greeting'
version = '1.0'
from components.java
}
}
}
Now, I have runner application to just run the greet task
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath 'standalone.plugin2.greeting:standalone-plugin2:1.0'
}
}
apply plugin: 'standalone.plugin2.greeting'
With apply plugin natation it works OK, but when I use plugins notation instead like this:
plugins {
id 'standalone.plugin2.greeting' version '1.0'
}
it doesn't work.
The error message is:
* What went wrong:
Plugin [id: 'standalone.plugin2.greeting', version: '1.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'standalone.plugin2.greeting:standalone.plugin2.greeting.gradle.plugin:1.0')
Searched in the following repositories:
Gradle Central Plugin Repository
What is the difference? According to documentation apply plugin is old and should not be used.
Before the introduction of the plugins block, plugin dependencies had to be resolved in the same way as regular project dependencies using a combination of repositories and dependencies. Since they need to be resolved before running the actual build script, they need to be defined in the special buildscript block:
buildscript {
repositories {
// define repositories for build script dependencies
}
dependencies {
// define build script dependencies (a.k.a. plugins)
}
}
repositories {
// define repositories for regular project dependencies
}
dependencies {
// define regular project dependencies
}
Once the dependencies were resolved, they could be applied using apply plugin:.
By default, the new plugins block just resolves the plugins from the Gradle Plugin Repository. This is a regular Maven repository, so it can be used using the old way, too:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
In your case, the plugin only exists in mavenLocal, so the plugins block cannot resolve it, as it only looks into the Gradle Central Plugin Repository. You may use the pluginManagement block to resolve plugins from custom repositories.
As described in the article linked above, it is necessary to create a link between the plugin identifier (used inside the plugins block) and the Maven coordinates that provide the respective plugin. To create this link a marker artifact following a specific convention must be published. The Gradle Plugin Development Plugin automatically publishes this marker artifact if used in combination with the Maven Publish Plugin.

Gradle doesn't find plugins: org.jetbrains.kotlin.jvm and kotlin2js

I'm a beginner in gradle, using version 4.8.
Whatever I do , the plugins are never found. I get this error message:
Plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.3.20'] was not found in any of the following sources:
Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.3.20')
Searched in the following repositories:
Gradle Central Plugin Repository
No matter how many repositories I add, it seems it is only looking in "Gradle Central Plugin Repository"
My gradle.build file:
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20"
classpath "org.jetbrains.kotlin.jvm:kotlin-gradle-plugin:1.3.20"
}
}
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.20'
id 'kotlin2js' version '1.3.20'
}
Can you help me?
Try the following gradle.build configuration:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20"
}
}
plugins {
id 'java'
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
}
When you include the plugin by id, it seems Gradle wants to retrieve the plugin from the Gradle plugin portal, but the Kotlin plugin is not there, it's part of the buildscript dependency. Using it with the apply plugin works. You can also find a slightly different working example here.
I had similar problem because i forgot about proxy settings like systemProp.https.proxyHost and systemProp.http.proxyHost and etc. that was set in ~/.gradle/gradle.properties.
I fixed configuration and plugin was successfully dowlnloaded
Check gradle.properties and try to add correct proxy settings if you behind firewall or escape this settings if you not.
you need to add repository mavenCentral() to the buildscript dependencies.
for example: kotlin-gradle-plugin:1.3.20. also the documentation hints for that.
Go to your project and then to the Gradle script. In Gradle, Go to Setting.Gradle and change the Fist Bitray Url to https://plugins.gradle.org/m2/.

Gradle subproject ordering and generate jar (spring boot + angular)

I am a Gradle newby. I have the following project setup:
Root
core: contains spring boot 2 application
ui: angular 5 front-end application
Goal: I want to run 'gradle build' from my root folder and it should contain one jar file which includes the Angular app.
I got the 'ui' covered:
apply plugin: "com.moowork.node"
buildscript {
repositories globalRepositories
dependencies {
classpath "com.moowork.gradle:gradle-node-plugin:1.2.0"
}
}
node {
// based on current version of Angular 5
version = "8.9.1"
npmVersion = "5.6.0"
download = true
}
task buildAngular(type: NpmTask) {
args = ['run', 'build']
}
buildAngular.dependsOn(npm_install)
build.dependsOn(buildAngular)
The above gradle definition will build and generate the Angular files in the static backend core application.
The 'core' gradle build file looks like this (I excluded the dependencies), nothing special:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'xxx.xxxxxx'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
buildscript {
repositories globalRepositories
ext {
springBootVersion = '2.0.0.M7'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
jar {
baseName = 'spring-boot-angular'
version = '1.0.0'
}
How can I make this possible? I want the following actions to be triggered when I run 'gradle build' from my root project:
run first 'gradle build' in ui
then second run 'gradle build' in core
use the generated jar file from 'core' as the end result
I can't stand the groovy like syntax, can't wait for Gradle Kotlin DSL to mature :P
Hope somebody can help. I will open source this setup (together with Spring 5, Hibernate 5 and flyway) when I get this up and ready. Thanks in advance.
You need to include the result of the frontend (ui) buildAngular task inside the jar generated in the backend (core) build:
bootJar {
dependsOn ':ui:buildAngular'
into('BOOT-INF/classes/static') {
from "${project(':ui').projectDir}/dist"
}
}
The fact that the bootJar task now depends on the buildAngular task of the frontend will make gradle order them as needed.
You can browse this project of mine to have an example using basically the same setup (except it uses yarn instead of npm to resolve dependencies)
Answer of JB Nizet should work. You can also add the following in the root gradle file:
build.dependsOn("core:build").mustRunAfter("ui:build")
Above answer is in my opinion cleaner.

Applying Gradle Dependency-Check plugin

I am trying to use the dependency.check from the following link and have been unable to get it to run properly (at all) when following the instructions given.
https://github.com/jeremylong/DependencyCheck/tree/master/dependency-check-gradle
When trying to build with the apply plugin and additional dependency the fails on startup and it throws the following error.
Where:
Build file '/Users/aaron/work/backups/eiss/build.gradle' line: 25
What went wrong:
A problem occurred evaluating root project 'eiss'.
Failed to apply plugin [id 'dependency.check']
Plugin with id 'dependency.check' not found.
I made a little progress when making some changes but was still ultimately unsuccessful.
First, I commented out the apply plugin line.
Next, I switched:
classpath "com.thoughtworks.tools:dependency-check:0.0.7"
over to:
compile "com.thoughtworks.tools:dependency-check:0.0.7"
After these two changes it began recognizing the path and I was able to see it grabbing the items from the repository.
Even with the path correct I am still having issues with the apply plugin line with it throwing the same error whenever I place it into the script or even try to change the '.' in it into a '-' (both are used in the instructions and in different repository examples).
Any help on this issue would be appreciated! Thanks
lastly here is the build.gradle script. I didn't want to just leave this blob right in the center of the post.
defaultTasks 'assemble'
// For third party libs that are widely used, keep versions in one place
ext {
MONGO_JAVA_DRIVER = "org.mongodb:mongo-java-driver:2.12.3"
RABBITMQ_VERSION = "com.rabbitmq:amqp-client:3.4.3"
LOG4J = "log4j:log4j:1.2.16"
// For groovy there are multiple libs, just capture version number and use lib-name-$GROOVY_VERSION
GROOVY_VERSION = "2.3.6"
}
//
// Common settings for all projects
//
subprojects {
defaultTasks 'assemble'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'codenarc'
apply plugin: 'dependency.check'
targetCompatibility = "1.6"
sourceCompatibility = "1.6"
repositories {
mavenCentral()
}
dependencies {
compile LOG4J
compile "org.codehaus.groovy:groovy:${GROOVY_VERSION}"
compile "org.codehaus.groovy:groovy-json:${GROOVY_VERSION}"
compile "org.codehaus.groovy:groovy-templates:${GROOVY_VERSION}"
compile "com.thoughtworks.tools:dependency-check:0.0.7"
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile "org.codehaus.groovy:groovy-test:${GROOVY_VERSION}"
testCompile "org.hamcrest:hamcrest-core:1.3"
}
clean.doLast {
// The archive path is configured via the jar tasks. Can't use
// delete jar.archivePath because that will configure the delete with
// the wrong (default) path of build/libs/<component.jar>
jar.archivePath.delete()
jarSources.archivePath.delete()
}
//--------------------------------------------------------------------
// Run and test
//--------------------------------------------------------------------
test {
// Uncomment to see standard output when running tests
testLogging.showStandardStreams = true
// This causes tests to run even when nothing has changed
outputs.upToDateWhen { false }
maxParallelForks = 1
}
task runClass(dependsOn: 'classes', type: JavaExec) {
if (project.hasProperty('classToRun')) {
if (project.hasProperty('arguments')) {
args(arguments.split(','))
}
classpath = sourceSets.main.runtimeClasspath
main=classToRun
}
}
//run this task to create source jars
task jarSources(type:Jar){
destinationDir = new File(projectDir.parent + "/sourcelibs")
from sourceSets.main.allSource
classifier 'sources'
}
}
You added plugin dependency in a wrong place, to the dependencies of your project, not a build script itself, which will use it. Try to add buildscript dependencies, as it's made in the example of plugin installation
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.thoughtworks.tools:dependency-check:0.0.7'
}
}
And then return your apply plugin
apply plugin: 'dependency.check'
Though this is very old post, accepted answer is using legacy plugin application, whereas below could be used while using the plugins DSL: https://plugins.gradle.org/plugin/org.owasp.dependencycheck
plugins {
id "org.owasp.dependencycheck" version "7.3.0"
}
With recent version of gradle it is below steps
add id 'com.diffplug.spotless' version '6.3.0' in plugins section like
plugins {
id 'com.diffplug.spotless' version '6.3.0'
}
And define your task to generate required format reports. Here for e.g. xml and json will be generated along with the html report
dependencyCheck{
formats=['xml','json']
check.dependsOn(dependencyCheckAnalyze)
}
And this can be integrated with Sonar by adding below properties to sonare.properties file (Provide dependency plugin is installed already on the sonar)
sonar.dependencyCheck.xmlReportPath=build/reports/dependency-check-report.xml
sonar.dependencyCheck.jsonReportPath=build/reports/dependency-check-report.json
sonar.dependencyCheck.htmlReportPath=build/reports/dependency-check-report.html

Resources