Skip to content

Commit 1bb9a55

Browse files
authored
ANALYZERS-77 : Setup license packaging standard (#424)
1 parent e9e8b9c commit 1bb9a55

14 files changed

+1747
-3
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
env:
5050
BUILD_NUMBER: ${{ needs.build.outputs.build-number }}
5151
steps:
52+
- name: Config Git
53+
run: git config --global core.autocrlf input
5254
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
5355
- uses: jdx/mise-action@5ac50f778e26fac95da98d50503682459e86d566 # v3.2.0
5456
with:

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ https://redirect.sonarsource.com/plugins/xml.html
99
Issue tracking:
1010
https://jira.sonarsource.com/browse/SONARXML/
1111

12+
### Updating licenses:
13+
When dependencies change, update the committed license files using the `updateLicenses` profile:
14+
```sh
15+
mvn clean package -PupdateLicenses
16+
```
17+
This regenerates licenses in `sonar-xml-plugin/src/main/resources/licenses/` based on current project dependencies.
1218

1319
License
1420
--------
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright (C) 2021-2025 SonarSource SA
3+
* All rights reserved
4+
* mailto:info AT sonarsource DOT com
5+
*/
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
import java.util.stream.Stream;
15+
16+
/**
17+
* Validates that generated license files match committed license files.
18+
* Used during Maven verify phase to ensure license files are up-to-date.
19+
*/
20+
public final class LicenseValidator {
21+
22+
private LicenseValidator() {
23+
// Utility class
24+
}
25+
26+
public static void main(String[] args) {
27+
try {
28+
var arguments = parseArguments(args);
29+
var tempLicensesPath = Path.of(arguments.get("temp_licenses"));
30+
var committedLicensesPath = Path.of(arguments.get("committed_licenses"));
31+
32+
validateDirectoriesExist(tempLicensesPath, committedLicensesPath);
33+
34+
var result = compareDirectories(tempLicensesPath, committedLicensesPath);
35+
36+
if (result.hasErrors()) {
37+
printFailureMessage(result);
38+
System.exit(1);
39+
} else {
40+
System.out.println("[SUCCESS] License validation passed - generated files match committed files");
41+
System.exit(0);
42+
}
43+
} catch (IllegalArgumentException e) {
44+
System.err.println("Error: " + e.getMessage());
45+
printUsage();
46+
System.exit(1);
47+
} catch (IOException e) {
48+
System.err.println("Error: " + e.getMessage());
49+
System.exit(1);
50+
}
51+
}
52+
53+
static Map<String, String> parseArguments(String[] args) {
54+
var arguments = new java.util.HashMap<String, String>();
55+
for (var arg : args) {
56+
if (arg.startsWith("--")) {
57+
var parts = arg.substring(2).split("=", 2);
58+
if (parts.length == 2) {
59+
arguments.put(parts[0], parts[1]);
60+
}
61+
}
62+
}
63+
64+
if (!arguments.containsKey("temp_licenses") || !arguments.containsKey("committed_licenses")) {
65+
throw new IllegalArgumentException("Missing required arguments");
66+
}
67+
68+
return arguments;
69+
}
70+
71+
static void validateDirectoriesExist(Path tempLicenses, Path committedLicenses) throws IOException {
72+
if (!Files.exists(tempLicenses)) {
73+
throw new IOException(
74+
"Temporary licenses directory not found: " + tempLicenses + "\n" +
75+
"Please run: mvn clean package -PupdateLicenses"
76+
);
77+
}
78+
if (!Files.exists(committedLicenses)) {
79+
throw new IOException(
80+
"Committed licenses directory not found: " + committedLicenses + "\n" +
81+
"Please run: mvn clean package -PupdateLicenses"
82+
);
83+
}
84+
}
85+
86+
static ValidationResult compareDirectories(Path tempDir, Path committedDir) throws IOException {
87+
var tempFiles = buildFileMap(tempDir);
88+
var committedFiles = buildFileMap(committedDir);
89+
90+
var newFiles = new ArrayList<String>();
91+
var missingFiles = new ArrayList<String>();
92+
var differentFiles = new ArrayList<String>();
93+
94+
// Find new files (in temp but not in committed)
95+
for (var relativePath : tempFiles.keySet()) {
96+
if (!committedFiles.containsKey(relativePath)) {
97+
newFiles.add(relativePath);
98+
}
99+
}
100+
101+
// Find missing files (in committed but not in temp)
102+
for (var relativePath : committedFiles.keySet()) {
103+
if (!tempFiles.containsKey(relativePath)) {
104+
missingFiles.add(relativePath);
105+
}
106+
}
107+
108+
// Find files with different content
109+
for (var relativePath : tempFiles.keySet()) {
110+
if (committedFiles.containsKey(relativePath)) {
111+
var tempFile = tempFiles.get(relativePath);
112+
var committedFile = committedFiles.get(relativePath);
113+
if (Files.mismatch(tempFile, committedFile) != -1L) {
114+
differentFiles.add(relativePath);
115+
}
116+
}
117+
}
118+
119+
newFiles.sort(String::compareTo);
120+
missingFiles.sort(String::compareTo);
121+
differentFiles.sort(String::compareTo);
122+
123+
return new ValidationResult(newFiles, missingFiles, differentFiles);
124+
}
125+
126+
static Map<String, Path> buildFileMap(Path rootDir) throws IOException {
127+
try (Stream<Path> paths = Files.walk(rootDir)) {
128+
return paths
129+
.filter(Files::isRegularFile)
130+
.collect(Collectors.toMap(
131+
path -> rootDir.relativize(path).toString().replace('\\', '/'),
132+
path -> path
133+
));
134+
}
135+
}
136+
137+
static void printFailureMessage(ValidationResult result) {
138+
System.err.println("[FAILURE] License validation failed!");
139+
System.err.println();
140+
141+
if (!result.newFiles().isEmpty()) {
142+
System.err.println("New files in generated licenses (not in committed):");
143+
for (var file : result.newFiles()) {
144+
System.err.println(" + " + file);
145+
}
146+
System.err.println();
147+
}
148+
149+
if (!result.missingFiles().isEmpty()) {
150+
System.err.println("Missing files in generated licenses (present in committed):");
151+
for (var file : result.missingFiles()) {
152+
System.err.println(" - " + file);
153+
}
154+
System.err.println();
155+
}
156+
157+
if (!result.differentFiles().isEmpty()) {
158+
System.err.println("Files with different content:");
159+
for (var file : result.differentFiles()) {
160+
System.err.println(" ~ " + file);
161+
}
162+
System.err.println();
163+
}
164+
165+
System.err.println("To fix this, run: mvn clean package -PupdateLicenses");
166+
}
167+
168+
static void printUsage() {
169+
System.err.println("Usage: LicenseValidator --temp_licenses=<path> --committed_licenses=<path>");
170+
}
171+
172+
record ValidationResult(
173+
List<String> newFiles,
174+
List<String> missingFiles,
175+
List<String> differentFiles
176+
) {
177+
boolean hasErrors() {
178+
return !newFiles.isEmpty() || !missingFiles.isEmpty() || !differentFiles.isEmpty();
179+
}
180+
}
181+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The plugin JAR contains a `licenses/` folder which contains the licenses of all the used libraries.
2+
These licenses have to be txt files. However, some libraries ship html files, in which case they need to be overwritten.
3+
4+
The files which overwrite the html file are located in this folder. The overwriting itself is configured
5+
in the `pom.xml` of the plugin module.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
This copy of StaxMate processing library is licensed under
2+
BSD License ("new BSD").
3+
See http://www.opensource.org/licenses/bsd-license.php
4+
for details.
5+
6+
Full license text is as follows:
7+
8+
--------------------------------------------------------------------------
9+
* Copyright (c) 2007, Tatu Saloranta
10+
* All rights reserved.
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
* * Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* * Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in the
18+
* documentation and/or other materials provided with the distribution.
19+
* * Neither the name of the <organization> nor the
20+
* names of its contributors may be used to endorse or promote products
21+
* derived from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
24+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26+
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
27+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
--------------------------------------------------------------------------

0 commit comments

Comments
 (0)