Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/actions/apache-maven-build-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Apache Maven Build Cache
description: Restore Maven module results for one CI job

inputs:
namespace:
description: CI job identity
required: true

outputs:
key:
description: Key to save after the job succeeds
value: ${{ steps.key.outputs.key }}

runs:
using: composite
steps:
- name: Identify cache environment
id: key
shell: bash
env:
CACHE_NAMESPACE: ${{ inputs.namespace }}
CONFIG_HASH: ${{ hashFiles('.mvn/extensions.xml', '.mvn/maven-build-cache-config.xml') }}
POM_HASH: ${{ hashFiles('**/pom.xml', 'docs/java-custom-rules-example/pom_SQ_10_6_LATEST.xml') }}
run: |
runtime_hash="$({ java --version; mvn --version; } | sha256sum | cut -c1-16)"
base="apache-maven-build-cache-1.3.0-${CACHE_NAMESPACE}-${RUNNER_OS}-${RUNNER_ARCH}-${runtime_hash}-${CONFIG_HASH}"
echo "key=${base}-${POM_HASH}-${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
echo "pom_prefix=${base}-${POM_HASH}-" >> "$GITHUB_OUTPUT"
echo "config_prefix=${base}-" >> "$GITHUB_OUTPUT"
- name: Restore Apache Maven build cache
uses: actions/cache/restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: ~/.m2/build-cache
key: ${{ steps.key.outputs.key }}
restore-keys: |
${{ steps.key.outputs.pom_prefix }}
${{ steps.key.outputs.config_prefix }}
63 changes: 56 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
outputs:
build-number: ${{ steps.build-maven.outputs.BUILD_NUMBER }}
deployed: ${{ steps.build-maven.outputs.deployed }}
project-version: ${{ steps.build-maven.outputs.project-version }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: jdx/mise-action@6612f0ef07ed2a736cd1e00a94a065522dd41195 # v5.2.5
Expand All @@ -48,6 +49,7 @@ jobs:
use-develocity: ${{ env.USE_DEVELOCITY }}
develocity-url: ${{ env.DEVELOCITY_URL }}
maven-args: >
--define maven.build.cache.enabled=false
--define maven.test.skip=true
--define sonar.skip=true
--projects !java-checks-test-sources/aws,!java-checks-test-sources/default,!java-checks-test-sources/java-17,!java-checks-test-sources/spring-3.2,!java-checks-test-sources/spring-web-4.0
Expand Down Expand Up @@ -148,7 +150,7 @@ jobs:
if [[ "${RUNNER_OS}" != "Windows" ]]; then
extra_args+=(-Dparallel=methods -DuseUnlimitedThreads=true)
fi
mvn "-Dorchestrator.configUrl=$ORCHESTRATOR_CONFIG_URL" "-Dorchestrator.home=$ORCHESTRATOR_HOME" package ${MAVEN_OUTPUT_ARGS} "-Pit-ruling,${{ matrix.item.profile }}" -Dsonar.runtimeVersion=${{ matrix.item.sq_version }} -Dmaven.test.redirectTestOutputToFile=false "${extra_args[@]}"
mvn "-Dorchestrator.configUrl=$ORCHESTRATOR_CONFIG_URL" "-Dorchestrator.home=$ORCHESTRATOR_HOME" package ${MAVEN_OUTPUT_ARGS} "-Pit-ruling,${{ matrix.item.profile }}" -Dsonar.runtimeVersion=${{ matrix.item.sq_version }} -Dmaven.test.redirectTestOutputToFile=false -Dmaven.build.cache.enabled=false "${extra_args[@]}"
- name: Upload Actual Results On Failure
if: failure()
uses: ./.github/actions/upload-actual
Expand Down Expand Up @@ -276,7 +278,7 @@ jobs:
ORCHESTRATOR_HOME: ${{ github.event_name == 'pull_request' && format('{0}/orchestrator-production', runner.temp) || env.ORCHESTRATOR_HOME }}
run: |
cd its/plugin
mvn "-Dorchestrator.configUrl=$ORCHESTRATOR_CONFIG_URL" "-Dorchestrator.home=$ORCHESTRATOR_HOME" package ${MAVEN_OUTPUT_ARGS} -Pit-plugin -Dsonar.runtimeVersion=${{ matrix.sq_version }} -Dmaven.test.redirectTestOutputToFile=false -Dparallel=classes -DuseUnlimitedThreads=true
mvn "-Dorchestrator.configUrl=$ORCHESTRATOR_CONFIG_URL" "-Dorchestrator.home=$ORCHESTRATOR_HOME" package ${MAVEN_OUTPUT_ARGS} -Pit-plugin -Dsonar.runtimeVersion=${{ matrix.sq_version }} -Dmaven.test.redirectTestOutputToFile=false -Dparallel=classes -DuseUnlimitedThreads=true -Dmaven.build.cache.enabled=false

sanity:
name: Sanity Test
Expand Down Expand Up @@ -305,25 +307,42 @@ jobs:
namespace: sanity
- uses: SonarSource/ci-github-actions/config-maven@v2
with:
working-directory: .mvn
repox-url: https://repox-internal.dev.sonar.build
artifactory-reader-role: private-reader
use-develocity: ${{ env.USE_DEVELOCITY }}
develocity-url: ${{ env.DEVELOCITY_URL }}
disable-caching: 'true'
- name: Restore Apache Maven build cache
id: apache-build-cache
uses: ./.github/actions/apache-maven-build-cache
with:
namespace: sanity-sources
- name: Compile Test Sources
env:
SONAR_HOST_URL: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_HOST_URL }}
SONAR_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_TOKEN }}
working-directory: java-checks-test-sources
run: |
mvn clean compile --batch-mode
mvn clean compile --batch-mode -Dmaven.build.cache.enabled=true -Ddevelocity.cache.local.enabled=false -Ddevelocity.cache.remote.enabled=false
- name: Sanity Test
env:
SONAR_HOST_URL: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_HOST_URL }}
SONAR_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_TOKEN }}
MAVEN_OPTS: "-Xmx8g"
DEPLOYED_PROJECT_VERSION: ${{ needs.build.outputs.project-version }}
run: |
mvn verify ${MAVEN_OUTPUT_ARGS} -f sonar-java-plugin/pom.xml -Psanity -Dtest=SanityTest
mvn org.codehaus.mojo:versions-maven-plugin:2.7:set -DnewVersion="$DEPLOYED_PROJECT_VERSION" -DgenerateBackupPoms=false -Dmaven.build.cache.enabled=false
mvn verify ${MAVEN_OUTPUT_ARGS} -f sonar-java-plugin/pom.xml -Psanity -Dtest=SanityTest -Dmaven.build.cache.enabled=false
- name: Save Apache Maven build cache
if: >-
github.repository == 'SonarSource/sonar-java' &&
((github.event_name == 'push' && github.ref == 'refs/heads/master') ||
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository))
uses: actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: ~/.m2/build-cache
key: ${{ steps.apache-build-cache.outputs.key }}

test-analyze:
name: Test Analyze
Expand Down Expand Up @@ -398,28 +417,42 @@ jobs:
SONAR_HOST_URL: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_HOST_URL }}
SONAR_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_TOKEN }}
JAVA_TOOL_OPTIONS: "" # Set an empty value to avoid issues with runners hanging and significantly slowing down builds
- name: Restore Apache Maven build cache
id: apache-build-cache
uses: ./.github/actions/apache-maven-build-cache
with:
namespace: custom-rules
- name: Build Java Custom Rules Example
env:
SONAR_HOST_URL: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_HOST_URL }}
SONAR_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).SONAR_TOKEN }}
working-directory: docs/java-custom-rules-example
run: |
mvn clean package -f pom_SQ_10_6_LATEST.xml --batch-mode
mvn clean package -f pom_SQ_10_6_LATEST.xml --batch-mode -Dmaven.build.cache.enabled=true -Ddevelocity.cache.local.enabled=false -Ddevelocity.cache.remote.enabled=false
- name: Check License Compliance
run: |
# See https://xtranet.sonarsource.com/display/DEV/Open+Source+Licenses
mvn org.codehaus.mojo:license-maven-plugin:aggregate-add-third-party \
--batch-mode \
"-Dlicense.missingFile=${PWD}/missing-dep-licenses.properties" \
-DuseMissingFile
- name: Save Apache Maven build cache
if: >-
github.repository == 'SonarSource/sonar-java' &&
((github.event_name == 'push' && github.ref == 'refs/heads/master') ||
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository))
uses: actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: ~/.m2/build-cache
key: ${{ steps.apache-build-cache.outputs.key }}

qa-os-win:
name: Build and Unit Test on Windows
# No dependency on build step, because we do not need the build number.
runs-on: warp-custom-windows-2022-m
permissions:
id-token: write # Required for Vault OIDC authentication
contents: write # Required for repository access and tagging
contents: read
steps:
- name: Config Git
run: git config --global core.autocrlf input
Expand All @@ -430,19 +463,35 @@ jobs:
- uses: ./.github/actions/maven-cache
with:
namespace: qa-os-win
- name: Restore Apache Maven build cache
id: apache-build-cache
uses: ./.github/actions/apache-maven-build-cache
with:
namespace: qa-os-win
- name: Configure Maven
uses: SonarSource/ci-github-actions/config-maven@v2
with:
working-directory: .mvn
repox-url: https://repox-internal.dev.sonar.build
artifactory-reader-role: private-reader
use-develocity: ${{ env.USE_DEVELOCITY }}
develocity-url: ${{ env.DEVELOCITY_URL }}
disable-caching: 'true'
- name: Run Maven
shell: bash
env:
JAVA_TOOL_OPTIONS: ""
MAVEN_OPTS: "-Xmx8g"
run: mvn clean verify --batch-mode
run: mvn clean verify --batch-mode -Dmaven.build.cache.enabled=true -Ddevelocity.cache.local.enabled=false -Ddevelocity.cache.remote.enabled=false
- name: Save Apache Maven build cache
if: >-
github.repository == 'SonarSource/sonar-java' &&
((github.event_name == 'push' && github.ref == 'refs/heads/master') ||
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository))
uses: actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: ~/.m2/build-cache
key: ${{ steps.apache-build-cache.outputs.key }}

promote:
needs:
Expand Down
5 changes: 5 additions & 0 deletions .mvn/extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
<artifactId>develocity-maven-extension</artifactId>
<version>1.22.2</version>
</extension>
<extension>
<groupId>org.apache.maven.extensions</groupId>
<artifactId>maven-build-cache-extension</artifactId>
<version>1.3.0</version>
</extension>
</extensions>
54 changes: 54 additions & 0 deletions .mvn/maven-build-cache-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0 https://maven.apache.org/xsd/build-cache-config-1.2.0.xsd">
<configuration>
<enabled>false</enabled>
<attachedOutputs>
<dirNames>
<dirName>classes</dirName>
<dirName>test-classes</dirName>
<dirName>generated-sources</dirName>
<dirName>generated-resources</dirName>
<dirName>surefire-reports</dirName>
<dirName>test-jars</dirName>
<dirName>test-projects</dirName>
<dirName>temp-licenses</dirName>
<dirName glob="test-classpath.txt"></dirName>
<dirName glob="jacoco.exec"></dirName>
</dirNames>
</attachedOutputs>
</configuration>
<input>
<global>
<glob>*</glob>
<includes>
<include>src/main</include>
<include>src/test</include>
<include>LICENSE.txt</include>
<include>../LICENSE.txt</include>
<include>license-resources</include>
</includes>
</global>
</input>
<executionControl>
<runAlways>
<goalsLists>
<goalsList artifactId="maven-install-plugin">
<goals><goal>install</goal></goals>
</goalsList>
<goalsList artifactId="maven-deploy-plugin">
<goals><goal>deploy</goal></goals>
</goalsList>
</goalsLists>
<executions>
<execution artifactId="license-maven-plugin">
<execIds><execId>download-licenses</execId></execIds>
</execution>
<execution artifactId="exec-maven-plugin">
<execIds><execId>validate-license-files</execId></execIds>
</execution>
</executions>
</runAlways>
</executionControl>
</cache>
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ For step-by-step guidance on implementing a new rule (including metadata generat

---

# Maven Development Builds

Always pass `-Dmaven.build.cache.enabled=true` to Maven commands during local development. Keep the existing goal, profiles, and test flags for the task; the extension caches modules and does not select individual tests. Use `-Dmaven.build.cache.enabled=false` for uncached reference or release checks. See `docs/maven-build-cache.md` for cache scope and rollback.

---

# Repository Structure

## Maven Module Structure
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ for guidance on when samples belong in the dedicated test-source modules instead

To build the plugin and run its unit tests, execute this command from the project's root directory:

mvn clean install
mvn clean install -Dmaven.build.cache.enabled=true

Always pass `-Dmaven.build.cache.enabled=true` for local development Maven builds. Use `-Dmaven.build.cache.enabled=false` for uncached reference or release builds. See [the Maven build cache guide](docs/maven-build-cache.md) for cache scope, isolation, and rollback.

### Note that
Running unit tests within the IDE might incur in some issues because of the way the project is built with Maven.
Expand Down
26 changes: 26 additions & 0 deletions docs/java-custom-rules-example/pom_SQ_10_6_LATEST.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Make sure to be compatible with java 8 -->
<maven.compiler.release>8</maven.compiler.release>
<maven.test.skip>false</maven.test.skip>
<maven.test.skip.exec>false</maven.test.skip.exec>
<skipTests>false</skipTests>
<maven.test.failure.ignore>false</maven.test.failure.ignore>
</properties>

<dependencies>
Expand Down Expand Up @@ -140,13 +144,20 @@
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.6</version>
<configuration>
<skip>${maven.test.skip}</skip>
<skipExec>${maven.test.skip.exec}</skipExec>
<skipTests>${skipTests}</skipTests>
<testFailureIgnore>${maven.test.failure.ignore}</testFailureIgnore>
</configuration>
</plugin>

<plugin>
Expand Down Expand Up @@ -233,4 +244,19 @@
</plugins>
</build>

<profiles>
<profile>
<id>partial-test-selection-no-cache</id>
<activation>
<property>
<name>test</name>
</property>
</activation>
<properties>
<maven.build.cache.skipCache>true</maven.build.cache.skipCache>
<maven.build.cache.skipSave>true</maven.build.cache.skipSave>
</properties>
</profile>
</profiles>

</project>
Loading
Loading