Skip to content

Commit

Permalink
TS-41699 Migrate maven plugin build to Gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
DreierF committed Feb 2, 2025
1 parent 45d47e3 commit 42bfbed
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 962 deletions.
4 changes: 2 additions & 2 deletions agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private static LoggingUtils.LoggingResources initializeFallbackLogging(String pr
loggingConfigLine = FileSystemUtils.readLinesUTF8(configFile).stream()
.filter(line -> line.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "="))
.findFirst();
} catch (IOException | AgentOptionParseException e) {
} catch (IOException e) {
delayedLogger.error("Failed to load configuration from " + configFileValue + ": " + e.getMessage(),
e);
}
Expand All @@ -265,7 +265,7 @@ private static LoggingUtils.LoggingResources createFallbackLoggerFromConfig(Stri
return LoggingUtils.initializeLogging(
new FilePatternResolver(delayedLogger).parsePath(AgentOptionsParser.LOGGING_CONFIG_OPTION,
configLocation));
} catch (IOException | AgentOptionParseException e) {
} catch (IOException e) {
String message = "Failed to load log configuration from location " + configLocation + ": " + e.getMessage();
delayedLogger.error(message, e);
// output the message to console as well, as this might
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.beust.jcommander.Parameters;
import com.teamscale.jacoco.agent.commandline.ICommand;
import com.teamscale.jacoco.agent.commandline.Validator;
import com.teamscale.jacoco.agent.options.AgentOptionParseException;
import com.teamscale.jacoco.agent.options.ClasspathUtils;
import com.teamscale.jacoco.agent.options.FilePatternResolver;
import com.teamscale.report.EDuplicateClassFileBehavior;
Expand All @@ -20,6 +19,7 @@
import org.conqat.lib.commons.string.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,7 +93,7 @@ public class ConvertCommand implements ICommand {
private int splitAfter = 5000;

/** @see #classDirectoriesOrZips */
public List<File> getClassDirectoriesOrZips() throws AgentOptionParseException {
public List<File> getClassDirectoriesOrZips() throws IOException {
return ClasspathUtils
.resolveClasspathTextFiles("class-dir", new FilePatternResolver(new CommandLineLogger()),
classDirectoriesOrZips);
Expand Down Expand Up @@ -168,4 +168,4 @@ public void run() throws Exception {
converter.runJaCoCoReportGeneration();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ public class AgentOptionParseException extends Exception {
*/
private static final long serialVersionUID = 1L;

/**
* Constructor.
*/
public AgentOptionParseException(String message) {
super(message);
}

/**
* Constructor.
*/
public AgentOptionParseException(Exception e) {
super(e.getMessage(), e);
}

public AgentOptionParseException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.teamscale.jacoco.agent.upload.artifactory.ArtifactoryConfig.ARTIFACTORY_GIT_PROPERTIES_COMMIT_DATE_FORMAT_OPTION;
import static com.teamscale.jacoco.agent.upload.artifactory.ArtifactoryConfig.ARTIFACTORY_GIT_PROPERTIES_JAR_OPTION;
Expand Down Expand Up @@ -68,10 +66,12 @@ public class AgentOptionsParser {
* @param environmentConfigId The Profiler configuration ID given via an environment variable.
* @param environmentConfigFile The Profiler configuration file given via an environment variable.
*/
public static Pair<AgentOptions, List<Exception>> parse(String optionsString, String environmentConfigId, String environmentConfigFile,
public static Pair<AgentOptions, List<Exception>> parse(String optionsString, String environmentConfigId,
String environmentConfigFile,
TeamscaleCredentials credentials,
ILogger logger) throws AgentOptionParseException, AgentOptionReceiveException {
AgentOptionsParser parser = new AgentOptionsParser(logger, environmentConfigId, environmentConfigFile, credentials);
AgentOptionsParser parser = new AgentOptionsParser(logger, environmentConfigId, environmentConfigFile,
credentials);
AgentOptions options = parser.parse(optionsString);
return Pair.createPair(options, parser.getCollectedErrors());
}
Expand Down Expand Up @@ -221,8 +221,8 @@ private void handleOption(AgentOptions options,
value)) {
return;
}
if (key.startsWith("proxy-") && handleProxyOptions(options, StringUtils.stripPrefix(key, "proxy-"), value,
filePatternResolver)) {
if (key.startsWith("proxy-") && handleProxyOptions(options, StringUtils.stripPrefix(key, "proxy-"), value
)) {
return;
}
if (handleAgentOptions(options, key, value)) {
Expand All @@ -231,8 +231,7 @@ private void handleOption(AgentOptions options,
throw new AgentOptionParseException("Unknown option: " + key);
}

private boolean handleProxyOptions(AgentOptions options, String key, String value,
FilePatternResolver filePatternResolver) throws AgentOptionParseException {
private boolean handleProxyOptions(AgentOptions options, String key, String value) throws AgentOptionParseException {
String httpsPrefix = ProxySystemProperties.Protocol.HTTPS + "-";
if (key.startsWith(httpsPrefix)
&& options.getTeamscaleProxyOptions(ProxySystemProperties.Protocol.HTTPS)
Expand All @@ -250,7 +249,7 @@ private boolean handleProxyOptions(AgentOptions options, String key, String valu
}

if (key.equals("password-file")) {
Path proxyPasswordPath = filePatternResolver.parsePath(key, value);
Path proxyPasswordPath = parsePath(key, value);
options.getTeamscaleProxyOptions(ProxySystemProperties.Protocol.HTTPS)
.setProxyPasswordPath(proxyPasswordPath);
options.getTeamscaleProxyOptions(ProxySystemProperties.Protocol.HTTP)
Expand Down Expand Up @@ -300,10 +299,10 @@ private boolean handleAgentOptions(AgentOptions options, String key, String valu
storeConfigId(options, value);
return true;
case CONFIG_FILE_OPTION:
readConfigFromFile(options, filePatternResolver.parsePath(key, value).toFile());
readConfigFromFile(options, parsePath(key, value).toFile());
return true;
case LOGGING_CONFIG_OPTION:
options.loggingConfig = filePatternResolver.parsePath(key, value);
options.loggingConfig = parsePath(key, value);
return true;
case "interval":
options.dumpIntervalInMinutes = parseInt(key, value);
Expand All @@ -312,7 +311,7 @@ private boolean handleAgentOptions(AgentOptions options, String key, String valu
options.validateSsl = Boolean.parseBoolean(value);
return true;
case "out":
options.setParentOutputDirectory(filePatternResolver.parsePath(key, value));
options.setParentOutputDirectory(parsePath(key, value));
return true;
case "upload-metadata":
try {
Expand Down Expand Up @@ -363,8 +362,12 @@ private boolean handleAgentOptions(AgentOptions options, String key, String valu
return true;
case "class-dir":
List<String> list = splitMultiOptionValue(value);
options.classDirectoriesOrZips = ClasspathUtils.resolveClasspathTextFiles(key, filePatternResolver,
list);
try {
options.classDirectoriesOrZips = ClasspathUtils.resolveClasspathTextFiles(key, filePatternResolver,
list);
} catch (IOException e) {
throw new AgentOptionParseException(e);
}
return true;
case "http-server-port":
options.httpServerPort = parseInt(key, value);
Expand Down Expand Up @@ -479,6 +482,21 @@ private int parseInt(String key, String value) throws AgentOptionParseException
}
}

/**
* Interprets the given pattern as an Ant pattern and resolves it to one existing {@link Path}. If the given path is
* relative, it is resolved relative to the current working directory. If more than one file matches the pattern,
* one of the matching files is used without any guarantees as to which. The selection is, however, guaranteed to be
* deterministic, i.e. if you run the pattern twice and get the same set of files, the same file will be picked each
* time.
*/
public Path parsePath(String optionName, String pattern) throws AgentOptionParseException {
try {
return filePatternResolver.parsePath(optionName, pattern);
} catch (IOException e) {
throw new AgentOptionParseException(e);
}
}

/**
* Parses the given value as a URL.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ public boolean handleTeamscaleOptions(TeamscaleServer teamscaleServer,
teamscaleServer.commit = parseCommit(value);
return true;
case TEAMSCALE_COMMIT_MANIFEST_JAR_OPTION:
teamscaleServer.commit = getCommitFromManifest(
filePatternResolver.parsePath(key, value).toFile());
try {
teamscaleServer.commit = getCommitFromManifest(
filePatternResolver.parsePath(key, value).toFile());
} catch (IOException e) {
throw new AgentOptionParseException(e);
}
return true;
case "teamscale-message":
teamscaleServer.setMessage(value);
Expand All @@ -79,8 +83,12 @@ public boolean handleTeamscaleOptions(TeamscaleServer teamscaleServer,
teamscaleServer.repository = value;
return true;
case TEAMSCALE_REVISION_MANIFEST_JAR_OPTION:
teamscaleServer.revision = getRevisionFromManifest(
filePatternResolver.parsePath(key, value).toFile());
try {
teamscaleServer.revision = getRevisionFromManifest(
filePatternResolver.parsePath(key, value).toFile());
} catch (IOException e) {
throw new AgentOptionParseException(e);
}
return true;
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ void setUp() throws IOException {

/** Tests path resolution with absolute path. */
@Test
void testPathResolutionForAbsolutePath() throws AgentOptionParseException {
void testPathResolutionForAbsolutePath() throws IOException {
assertInputInWorkingDirectoryMatches(".", testFolder.getAbsolutePath(), "");
}

/** Tests path resolution with relative paths. */
@Test
void testPathResolutionForRelativePath() throws AgentOptionParseException {
void testPathResolutionForRelativePath() throws IOException {
assertInputInWorkingDirectoryMatches(".", ".", "");
assertInputInWorkingDirectoryMatches("plugins", "../file_with_manifest1.jar", "file_with_manifest1.jar");
}

/** Tests path resolution with patterns and relative paths. */
@Test
public void testPathResolutionWithPatternsAndRelativePaths() throws AgentOptionParseException {
public void testPathResolutionWithPatternsAndRelativePaths() throws IOException {
assertInputInWorkingDirectoryMatches(".", "plugins/file_*.jar", "plugins/file_with_manifest2.jar");
assertInputInWorkingDirectoryMatches(".", "*/file_*.jar", "plugins/file_with_manifest2.jar");
assertInputInWorkingDirectoryMatches("plugins/inner", "..", "plugins");
Expand All @@ -54,13 +54,13 @@ public void testPathResolutionWithPatternsAndRelativePaths() throws AgentOptionP

/** Tests path resolution with patterns and absolute paths. */
@Test
void testPathResolutionWithPatternsAndAbsolutePaths() throws AgentOptionParseException {
void testPathResolutionWithPatternsAndAbsolutePaths() throws IOException {
assertInputInWorkingDirectoryMatches("plugins", testFolder.getAbsolutePath() + "/plugins/file_*.jar",
"plugins/file_with_manifest2.jar");
}

private void assertInputInWorkingDirectoryMatches(String workingDir, String input,
String expected) throws AgentOptionParseException {
String expected) throws IOException {
File workingDirectory = new File(testFolder, workingDir);
File actualFile = getFilePatternResolverWithDummyLogger().parsePath("option-name", input, workingDirectory)
.toFile();
Expand All @@ -78,24 +78,24 @@ private static Path getNormalizedPath(File file) {
void testPathResolutionWithPatternErrorCases() {
assertThatThrownBy(
() -> getFilePatternResolverWithDummyLogger().parsePath("option-name", "**.war", testFolder))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(
"Invalid path given for option option-name: " +
"**.war. The pattern **.war did not match any files in");
.isInstanceOf(IOException.class).hasMessageContaining(
"Invalid path given for option option-name: " +
"**.war. The pattern **.war did not match any files in");
}

private static FilePatternResolver getFilePatternResolverWithDummyLogger() {
return new FilePatternResolver(new CommandLineLogger());
}

@Test
void resolveToMultipleFilesWithPattern() throws AgentOptionParseException {
void resolveToMultipleFilesWithPattern() throws IOException {
List<File> files = getFilePatternResolverWithDummyLogger()
.resolveToMultipleFiles("option-name", "**.jar", testFolder);
assertThat(files).hasSize(3);
}

@Test
void resolveToMultipleFilesWithoutPattern() throws AgentOptionParseException {
void resolveToMultipleFilesWithoutPattern() throws IOException {
List<File> files = getFilePatternResolverWithDummyLogger()
.resolveToMultipleFiles("option-name", "plugins/file_with_manifest2.jar", testFolder);
assertThat(files).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ plugins {

group = "com.teamscale"

repositories {
mavenCentral()
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/com.teamscale.publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ fun PublicationContainer.configureMavenPublication() {
pluginManager.withPlugin("java-library") {
from(components["java"])
}
pluginManager.withPlugin("org.gradlex.maven-plugin-development") {
from(components["java"])
}
}
}
}
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ okhttp = "4.12.0"
mockito = "5.15.2"
mockitoKotlin = "5.4.0"
picocli = "4.7.6"
maven = "3.9.9"

[libraries]
jetty-server = { module = "org.eclipse.jetty:jetty-server", version.ref = "jetty" }
Expand Down Expand Up @@ -80,9 +81,14 @@ mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "

springboot-loader = { module = "org.springframework.boot:spring-boot-loader", version = "3.4.2" }

maven-core = { module = "org.apache.maven:maven-core", version.ref = "maven" }
maven-pluginApi = { module = "org.apache.maven:maven-plugin-api", version.ref = "maven" }
maven-pluginAnnotations = { module = "org.apache.maven.plugin-tools:maven-plugin-annotations", version = "3.15.1" }

[plugins]
versions = { id = "com.github.ben-manes.versions", version = "0.52.0" }
markdownToPdf = { id = "de.fntsoftware.gradle.markdown-to-pdf", version = "1.1.0" }
nexusPublish = { id = "io.github.gradle-nexus.publish-plugin", version = "2.0.0" }
pluginPublish = { id = "com.gradle.plugin-publish", version = "1.3.1" }
gitProperties = { id = "com.gorylenko.gradle-git-properties", version = "2.4.2" }
mavenPluginDevelopment = { id = "org.gradlex.maven-plugin-development", version = "1.0.2" }
1 change: 1 addition & 0 deletions report-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
}

implementation(libs.jackson.databind)
implementation(libs.teamscaleLibCommons)

testImplementation(libs.jsonassert)
testImplementation(libs.teamscaleLibCommons)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ClasspathUtils {

/** Replaces all txt files in the given list with the file names written in the txt file separated by new lines. */
public static List<File> resolveClasspathTextFiles(String key, FilePatternResolver filePatternResolver,
List<String> patterns) throws AgentOptionParseException {
List<String> patterns) throws IOException {
List<File> resolvedPaths = new ArrayList<>();
for (String pattern : patterns) {
resolvedPaths.addAll(filePatternResolver.resolveToMultipleFiles(key, pattern));
Expand All @@ -29,12 +29,12 @@ public static List<File> resolveClasspathTextFiles(String key, FilePatternResolv
}

private static List<File> resolveClassPathEntries(String key, FilePatternResolver filePatternResolver,
File txtFile) throws AgentOptionParseException {
File txtFile) throws IOException {
List<String> filePaths;
try {
filePaths = FileSystemUtils.readLinesUTF8(txtFile);
} catch (IOException e) {
throw new AgentOptionParseException("Failed read class path entries from the provided " + txtFile +
throw new IOException("Failed read class path entries from the provided " + txtFile +
" in the `" + key + "` option.", e);
}
List<File> resolvedFiles = new ArrayList<>();
Expand Down
Loading

0 comments on commit 42bfbed

Please sign in to comment.