Skip to content

Commit

Permalink
add possibility to set environment variables by maven
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz-eitco-de committed Jun 18, 2024
1 parent 157be1e commit ddf0812
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
39 changes: 35 additions & 4 deletions src/main/java/de/eitco/cicd/dotnet/AbstractDotnetMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public abstract class AbstractDotnetMojo extends AbstractMojo {
@Parameter(defaultValue = "maven-nuget-local")
protected String localMavenNugetRepositoryName;

@Parameter(defaultValue = "${settings.localRepository}")
protected String localMavenNugetRepositoryBaseDirectory;

/**
* This parameter specifies custom properties given to dotnet via '-p:'. Keep in mind that the 'Version', 'Company',
* 'Description', 'RepositoryUrl' and 'AssemblyVersion' properties will be overwritten by their respective
Expand All @@ -52,6 +55,9 @@ public abstract class AbstractDotnetMojo extends AbstractMojo {
@Parameter
protected Map<String, String> customProperties = Map.of();

@Parameter
protected Map<String, String> environmentVariables = Map.of();

@Component(hint = "dotnet-security")
private SecDispatcher securityDispatcher;

Expand All @@ -67,6 +73,7 @@ protected DotnetExecutor newExecutor(boolean ignoreResult) {
targetDirectory,
projectVersion,
customProperties,
environmentVariables,
getLog(),
ignoreResult
);
Expand Down Expand Up @@ -95,18 +102,42 @@ protected Server findServer(String serverId) throws MojoExecutionException {
return server;
}

protected File getLocalNugetRepository() {
return new File(new File(settings.getLocalRepository()), "." + localMavenNugetRepositoryName);
protected File getResolvedNugetRepoDirectory() {

String baseDirectory = localMavenNugetRepositoryBaseDirectory;

for (Map.Entry<String, String> entry : System.getenv().entrySet()) {

String key = entry.getKey();
String value = entry.getValue();

baseDirectory = baseDirectory.replace("%" + key + "%", value);
}

for (Map.Entry<String, String> entry : environmentVariables.entrySet()) {

String key = entry.getKey();
String value = entry.getValue();

baseDirectory = baseDirectory.replace("%" + key + "%", value);
}

return getNugetRepoDirectory();
}

private File getNugetRepoDirectory() {

return new File(localMavenNugetRepositoryBaseDirectory, "." + localMavenNugetRepositoryName);
}

protected File createLocalNugetRepoDirectory() throws MojoExecutionException {
try {

File localNugetRepository = getLocalNugetRepository();
File localNugetRepository = getResolvedNugetRepoDirectory();

FileUtils.forceMkdir(localNugetRepository);

return localNugetRepository;
return getNugetRepoDirectory();

} catch (IOException e) {

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public record DotnetExecutor(
File targetDirectory,
String version,
Map<String, String> customProperties,
Map<String, String> environment,
Log log,
boolean ignoreResult
) {
Expand Down Expand Up @@ -68,6 +69,8 @@ private int execute(ExecutionOptions executionOptions, List<String> parameters,

builder.environment().put("DOTNET_CLI_TELEMETRY_OPTOUT", "TRUE");

environment.forEach((key, value) -> builder.environment().put(key, value));

try {

log.info("Executing command: " + presentCommand(command, obfuscation));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/eitco/cicd/dotnet/InitializeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void execute() throws MojoExecutionException {

File localNugetRepository = createLocalNugetRepoDirectory();

dotnetExecutor.upsertNugetSource(localNugetRepository.getAbsolutePath(), localMavenNugetRepositoryName, null, null);
dotnetExecutor.upsertNugetSource(localNugetRepository.getPath(), localMavenNugetRepositoryName, null, null);

for (Map.Entry<String, String> entry : nugetSources.entrySet()) {
String key = entry.getKey();
Expand Down

0 comments on commit ddf0812

Please sign in to comment.