Skip to content

Commit

Permalink
PGPShowMojo - Show information about artifact signature.
Browse files Browse the repository at this point in the history
connected: #169, #136
  • Loading branch information
slawekjaranowski committed Oct 24, 2020
1 parent 5069ba8 commit 28cd212
Show file tree
Hide file tree
Showing 26 changed files with 1,372 additions and 84 deletions.
3 changes: 3 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lombok.log.fieldName=LOGGER
lombok.addLombokGeneratedAnnotation=true
lombok.anyConstructor.addConstructorProperties=true
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

<!-- dependency coverage -->
<dependency>
Expand Down Expand Up @@ -270,6 +276,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/it/parentForTest/pom-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>maven-jar-plugin</artifactId>
<version>@maven-jar-plugin.version@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>@maven-release-plugin.version@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
Expand Down
16 changes: 16 additions & 0 deletions src/it/showSignature/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2020 Slawomir Jaranowski
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
invoker.goals = pgpverify:show -Dartifact=junit:junit:4.12 -DshowPom
41 changes: 41 additions & 0 deletions src/it/showSignature/pom-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2020 Slawomir Jaranowski
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>it-test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>

<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.simplify4u.plugins</groupId>
<artifactId>pgpverify-maven-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</build>
</project>
30 changes: 30 additions & 0 deletions src/it/showSignature/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

def buildLog = new File( basedir, 'build.log' ).text

assert buildLog.contains('type: jar')
assert buildLog.contains('type: pom')
assert buildLog.contains('version: 4.12')

assert buildLog.contains('PGP signature:')
assert buildLog.contains('keyId: 0xEFE8086F9E93774E')

assert buildLog.contains('PGP key:')
assert buildLog.contains('fingerprint: 0xD4C89EA4AAF455FD88B22087EFE8086F9E93774E')
assert buildLog.contains('master key: 0x58E79B6ABC762159DC0B1591164BD2247B936711')

assert buildLog.contains('[INFO] BUILD SUCCESS')
56 changes: 19 additions & 37 deletions src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
import javax.inject.Inject;

import io.vavr.control.Try;
import lombok.Setter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.simplify4u.plugins.keyserver.PGPKeysCache;
import org.simplify4u.plugins.keysmap.KeysMap;
import org.simplify4u.plugins.utils.PGPSignatureUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -41,36 +41,14 @@ public abstract class AbstractPGPMojo extends AbstractMojo {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPGPMojo.class);

@Inject
protected MavenSession session;

@Inject
protected PGPKeysCache pgpKeysCache;
protected final ArtifactResolver artifactResolver;

@Inject
protected KeysMap keysMap;
protected final PGPKeysCache pgpKeysCache;

/**
* <p>
* Specifies the location of a file that contains the map of dependencies to PGP key.
* </p>
*
* <p>
* This can be path to local file, path to file on plugin classpath or url address.
* </p>
*
* <p>
* <a href="keysmap-format.html">Format description.</a>
* </p>
*
* <p>
* You can use ready keys map: <a href="https://github.com/s4u/pgp-keys-map">https://github.com/s4u/pgp-keys-map</a>
* </p>
*
* @since 1.1.0
*/
@Parameter(property = "pgpverify.keysMapLocation", defaultValue = "")
private String keysMapLocation;
protected final PGPSignatureUtils pgpSignatureUtils;

protected final MavenSession session;

/**
* The directory for storing cached PGP public keys.
Expand Down Expand Up @@ -108,6 +86,7 @@ public abstract class AbstractPGPMojo extends AbstractMojo {
* @since 1.3.0
*/
@Parameter(property = "pgpverify.skip", defaultValue = "false")
@Setter
private boolean skip;

/**
Expand All @@ -128,6 +107,15 @@ public abstract class AbstractPGPMojo extends AbstractMojo {
@Parameter(property = "pgpverify.quiet", defaultValue = "false")
private boolean quiet;

@Inject
AbstractPGPMojo(ArtifactResolver artifactResolver, PGPKeysCache pgpKeysCache,
PGPSignatureUtils pgpSignatureUtils, MavenSession session) {
this.artifactResolver = artifactResolver;
this.pgpKeysCache = pgpKeysCache;
this.pgpSignatureUtils = pgpSignatureUtils;
this.session = session;
}

@Override
public final Log getLog() {
throw new UnsupportedOperationException("SLF4J should be used directly");
Expand All @@ -144,10 +132,6 @@ private void initPgpKeysCache() throws IOException {
pgpKeysCache.init(pgpKeysCachePath, pgpKeyServer, pgpKeyServerLoadBalance, proxyName);
}

private void initKeysMap() throws ResourceNotFoundException, IOException {
keysMap.load(keysMapLocation);
}

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {

Expand All @@ -156,10 +140,8 @@ public final void execute() throws MojoExecutionException, MojoFailureException
return;
}

Try.run(() -> {
initPgpKeysCache();
initKeysMap();
}).getOrElseThrow(e -> new MojoFailureException(e.getMessage(), e));
Try.run(this::initPgpKeysCache)
.getOrElseThrow(e -> new MojoFailureException(e.getMessage(), e));

executeConfiguredMojo();
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/simplify4u/plugins/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* Artifact resolver for project dependencies, build plug-ins, and build plug-in dependencies.
*/
@Named
final class ArtifactResolver {
public class ArtifactResolver {

private static final Logger LOG = LoggerFactory.getLogger(ArtifactResolver.class);

Expand Down Expand Up @@ -333,8 +333,12 @@ private Set<Artifact> resolveTransitively(Artifact artifact, SkipFilter dependen
throws MojoExecutionException {
final ArtifactFilter requestFilter = a -> !dependencyFilter.shouldSkipArtifact(a);
final ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setArtifact(artifact).setLocalRepository(localRepository).setRemoteRepositories(remoteRepositories)
.setResolutionFilter(requestFilter).setCollectionFilter(requestFilter).setResolveTransitively(true);
.setArtifact(artifact)
.setLocalRepository(localRepository)
.setRemoteRepositories(remoteRepositories)
.setResolutionFilter(requestFilter)
.setCollectionFilter(requestFilter)
.setResolveTransitively(true);
final ArtifactResolutionResult resolution = repositorySystem.resolve(request);
if (!resolution.isSuccess()) {
if (resolution.hasMissingArtifacts()) {
Expand All @@ -356,7 +360,7 @@ private Set<Artifact> resolveTransitively(Artifact artifact, SkipFilter dependen
}
}

private Artifact resolvePom(Artifact artifact) {
public Artifact resolvePom(Artifact artifact) {
final Artifact pomArtifact = repositorySystem.createProjectArtifact(artifact.getGroupId(),
artifact.getArtifactId(), artifact.getVersion());
final ArtifactResolutionResult result = request(pomArtifact, remoteRepositories);
Expand All @@ -367,7 +371,7 @@ private Artifact resolvePom(Artifact artifact) {
return pomArtifact;
}

private Artifact resolveArtifact(Artifact artifact) {
public Artifact resolveArtifact(Artifact artifact) {
final ArtifactResolutionResult result = request(artifact, remoteRepositories);
if (!result.isSuccess()) {
result.getExceptions().forEach(e -> LOG.warn("Failed to resolve {}: {}", artifact.getId(), e.getMessage()));
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/org/simplify4u/plugins/PGPMojoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2020 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simplify4u.plugins;


import java.util.Arrays;

/**
* Common runtime exception for Maven mojo.
*
* @author Slawomir Jaaranowski
*/
public class PGPMojoException extends RuntimeException {

private static class Formatter {

String message;
Throwable cause;

public Formatter(String messageToFormat, Object[] args) {
Object[] argsToFormat;
Object last = args.length > 0 ? args[args.length - 1] : null;
if (last instanceof Throwable) {
argsToFormat = Arrays.copyOf(args, args.length - 1);
cause = (Throwable) last;
} else {
argsToFormat = args;
}
message = String.format(messageToFormat, argsToFormat);
}
}

private static final long serialVersionUID = -2691735554566086599L;

public PGPMojoException() {
super();
}

public PGPMojoException(Throwable cause) {
super(cause);
}

public PGPMojoException(String message) {
super(message);
}

/**
* Message can contains {@link String#format(String, Object...)} placeholders.
* <p>
* If last argument is Throwable, then will be used as a cause of exception.
*
* @param message message of exception
* @param args additional args for message formatting
*/
public PGPMojoException(String message, Object... args) {
this(new Formatter(message, args));
}

private PGPMojoException(Formatter format) {
super(format.message, format.cause);
}
}
Loading

0 comments on commit 28cd212

Please sign in to comment.