Skip to content

Commit

Permalink
add push goal
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz-eitco-de committed Jun 4, 2024
1 parent 247ae58 commit c5da3ff
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ public record DotnetExecutor(

public int execute(String... parameters) throws MojoExecutionException {

return execute(List.of(parameters));
}

public int execute(List<String> parameters) throws MojoExecutionException {

ProcessBuilder builder = new ProcessBuilder();

builder.directory(workingDirectory);

List<String> command = buildCommand(List.of(parameters));
List<String> command = buildCommand(parameters);

builder.command(command);

Expand Down Expand Up @@ -68,11 +73,37 @@ public void build() throws MojoExecutionException {

public void pack(String packageId, String version, String vendor, String description, String repositoryUrl) throws MojoExecutionException {

execute("pack", "--no-build", "-p:PackageId=" + packageId, "-p:PackageVersion=" + version, "-p:Company=" + vendor, "-p:Description=" + description, "-p:RepositoryUrl=" + repositoryUrl);
execute(
"pack",
"--no-build",
"-p:PackageId=" + packageId,
"-p:PackageVersion=" + version,
"-p:Company=" + vendor,
"-p:Description=" + description,
"-p:RepositoryUrl=" + repositoryUrl,
"--output", targetDirectory.getPath()
);
}

public int test(String logger, String testResultDirectory) throws MojoExecutionException {

return execute("test", "--no-build", "--logger", logger, "--results-directory", testResultDirectory);
}

public void push(String apiKey, String repositoryUrl) throws MojoExecutionException {

List<String> parameters = new ArrayList<>(List.of("nuget", "push", targetDirectory.getPath() + "/**.nupkg"));

if (apiKey != null) {
parameters.add("--api-key");
parameters.add(apiKey);
}

if (repositoryUrl != null) {
parameters.add("--source");
parameters.add(repositoryUrl);
}

execute(parameters);
}
}
94 changes: 94 additions & 0 deletions src/main/java/de/eitco/cicd/dotnet/NugetPushMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package de.eitco.cicd.dotnet;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;

@Mojo(name = "push", defaultPhase = LifecyclePhase.DEPLOY)
public class NugetPushMojo extends AbstractDotnetMojo {

@Parameter
private String nugetServerId;

@Parameter
private String nugetServerUrl;

@Parameter
private String nugetSnapshotServerUrl;

@Parameter(defaultValue = "${settings}", readonly = true)
protected Settings settings;

@Parameter(defaultValue = "${project}", readonly = true)
protected MavenProject project;

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

@Override
public void execute() throws MojoExecutionException {

String apiKey = findApiKey();

String repositoryUrl = decideRepositoryUrl();

newExecutor().push(apiKey, repositoryUrl);

}

private String decideRepositoryUrl() {

boolean isSnapshot = project.getVersion().endsWith("-SNAPSHOT");

if (isSnapshot) {

return coalesce(nugetSnapshotServerUrl, nugetServerUrl, project.getDistributionManagement().getSnapshotRepository().getUrl(), project.getDistributionManagement().getRepository().getUrl());
}

return coalesce(nugetServerUrl, project.getDistributionManagement().getRepository().getUrl());
}

private String coalesce(String... strings) {

for (String string : strings) {

if (string != null) {

return string;
}
}

return null;
}

private String findApiKey() throws MojoExecutionException {

if (nugetServerId == null) {

return null;
}

Server server = settings.getServer(nugetServerId);

if (server == null) {

throw new MojoExecutionException("server " + nugetServerId + " not found");
}

try {

return securityDispatcher.decrypt(server.getPassword());

} catch (SecDispatcherException e) {

throw new MojoExecutionException(e);
}
}
}
21 changes: 21 additions & 0 deletions src/main/resource-templates/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<package>
${project.groupId}:${project.artifactId}:${project.version}:pack
</package>
<deploy>${project.groupId}:${project.artifactId}:${project.version}:push</deploy>
</phases>
</lifecycle>
</lifecycles>
Expand All @@ -36,5 +37,25 @@
<addedToClasspath>false</addedToClasspath>
</configuration>
</component>
<component>
<role>org.sonatype.plexus.components.sec.dispatcher.SecDispatcher</role>
<role-hint>dotnet-security</role-hint>
<implementation>org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher</implementation>
<requirements>
<requirement>
<role>org.sonatype.plexus.components.cipher.PlexusCipher</role>
<role-hint>dotnet-security</role-hint>
<field-name>_cipher</field-name>
</requirement>
</requirements>
<configuration>
<_configuration-file>~/.m2/settings-security.xml</_configuration-file>
</configuration>
</component>
<component>
<role>org.sonatype.plexus.components.cipher.PlexusCipher</role>
<role-hint>dotnet-security</role-hint>
<implementation>org.sonatype.plexus.components.cipher.DefaultPlexusCipher</implementation>
</component>
</components>
</component-set>

0 comments on commit c5da3ff

Please sign in to comment.