This is an automated email from the ASF dual-hosted git repository. rfscholte pushed a commit to branch maven-sign-plugin in repository https://gitbox.apache.org/repos/asf/maven-studies.git
commit 2d0fee06bb457ad97d46c87b9f9ce16ebe368d29 Author: Slawomir Jaranowski <s.jaranow...@gmail.com> AuthorDate: Sun Oct 4 20:53:01 2020 +0200 Fix after review --- .github/workflows/maven.yml | 2 +- lombok.config | 18 ------- pom.xml | 7 --- .../org/apache/maven/plugins/sign/SignMojo.java | 62 +++++++++++++--------- .../{SignResult.java => SignMojoException.java} | 24 ++++----- .../org/apache/maven/plugins/sign/SignResult.java | 45 ++++++++++++---- .../apache/maven/plugins/sign/pgp/PGPSigner.java | 14 +++-- .../sign/PGPSecretKeyInfoFromSettingsTest.java | 25 +++++++-- .../maven/plugins/sign/pgp/PGPSignerTest.java | 5 +- 9 files changed, 116 insertions(+), 86 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 9981d7b..eb80283 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -38,7 +38,7 @@ jobs: uses: actions/cache@v2.1.1 with: path: ~/.m2/repository - key: maven-${{ matrix.os }}-java${{ matrix.java }}-${{ hashFiles('**/pom.xml') }} + key: maven-sign-plugin-${{ matrix.os }}-java${{ matrix.java }}-${{ hashFiles('**/pom.xml') }} restore-keys: | maven-${{ matrix.os }}-java${{ matrix.java }}- maven-${{ matrix.os }}- diff --git a/lombok.config b/lombok.config deleted file mode 100644 index 767ef75..0000000 --- a/lombok.config +++ /dev/null @@ -1,18 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you 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. - -lombok.log.fieldName=LOGGER diff --git a/pom.xml b/pom.xml index f208d91..ede1744 100644 --- a/pom.xml +++ b/pom.xml @@ -81,13 +81,6 @@ </dependency> <dependency> - <groupId>org.projectlombok</groupId> - <artifactId>lombok</artifactId> - <version>1.18.12</version> - <scope>provided</scope> - </dependency> - - <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpg-jdk15on</artifactId> <version>1.66</version> diff --git a/src/main/java/org/apache/maven/plugins/sign/SignMojo.java b/src/main/java/org/apache/maven/plugins/sign/SignMojo.java index 0c9c8e4..e72e916 100644 --- a/src/main/java/org/apache/maven/plugins/sign/SignMojo.java +++ b/src/main/java/org/apache/maven/plugins/sign/SignMojo.java @@ -19,8 +19,6 @@ package org.apache.maven.plugins.sign; * under the License. */ -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; @@ -34,10 +32,14 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; import org.apache.maven.project.artifact.ProjectArtifact; import org.eclipse.aether.transform.FileTransformer; +import org.eclipse.aether.transform.TransformException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.inject.Inject; import java.io.BufferedInputStream; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; import java.nio.file.Paths; @@ -52,17 +54,21 @@ import java.util.Set; * * @author Slawomir Jaranowski */ -@Slf4j @Mojo( name = "sign", defaultPhase = LifecyclePhase.VERIFY ) public class SignMojo extends AbstractMojo { + private static final Logger LOGGER = LoggerFactory.getLogger( SignMojo.class ); + @Parameter( defaultValue = "${project}", readonly = true, required = true ) private MavenProject project; @Parameter( defaultValue = "${session}", required = true, readonly = true ) protected MavenSession session; + @Parameter( defaultValue = "pgpKey", required = true, property = "sign.serverId" ) + private String serverId; + @Inject private MavenProjectHelper projectHelper; @@ -74,7 +80,7 @@ public class SignMojo extends AbstractMojo try { pgpSigner = new PGPSigner( - new PGPSecretKeyInfoFromSettings( session.getSettings().getServer( "pgpKey" ) ) ); + new PGPSecretKeyInfoFromSettings( session.getSettings().getServer( serverId ) ) ); } catch ( PGPSignerException e ) { @@ -104,7 +110,6 @@ public class SignMojo extends AbstractMojo * @param artifact artifact to sign * @return sign result */ - @SneakyThrows private List<SignResult> signArtefact( Artifact artifact ) { LOGGER.info( "Sign artifact: {}", artifact ); @@ -123,23 +128,34 @@ public class SignMojo extends AbstractMojo List<SignResult> result = new ArrayList<>(); - if ( transformersForArtifact.isEmpty() ) - { - result.add( makeSign( new BufferedInputStream( new FileInputStream( srcArtifact.getFile() ) ), - srcArtifact.getArtifactId(), - srcArtifact.getClassifier(), - srcArtifact.getExtension() ) ); - } - else + try { - for ( FileTransformer fileTransformer : transformersForArtifact ) + if ( transformersForArtifact.isEmpty() ) { - org.eclipse.aether.artifact.Artifact dstArtifact = fileTransformer.transformArtifact( srcArtifact ); - result.add( makeSign( fileTransformer.transformData( srcArtifact.getFile() ), - dstArtifact.getArtifactId(), - dstArtifact.getClassifier(), - dstArtifact.getExtension() ) ); + try ( InputStream artifactInputStream = new BufferedInputStream( + new FileInputStream( srcArtifact.getFile() ) ) ) + { + result.add( makeSignature( artifactInputStream, + srcArtifact.getArtifactId(), + srcArtifact.getClassifier(), + srcArtifact.getExtension() ) ); + } } + else + { + for ( FileTransformer fileTransformer : transformersForArtifact ) + { + org.eclipse.aether.artifact.Artifact dstArtifact = fileTransformer.transformArtifact( srcArtifact ); + result.add( makeSignature( fileTransformer.transformData( srcArtifact.getFile() ), + dstArtifact.getArtifactId(), + dstArtifact.getClassifier(), + dstArtifact.getExtension() ) ); + } + } + } + catch ( IOException | PGPSignerException | TransformException e ) + { + throw new SignMojoException( e ); } return result; @@ -155,7 +171,7 @@ public class SignMojo extends AbstractMojo * @return result of signing * @throws PGPSignerException if some thing wrong */ - private SignResult makeSign( InputStream inputStream, String artifactId, String classifier, String extension ) + private SignResult makeSignature( InputStream inputStream, String artifactId, String classifier, String extension ) throws PGPSignerException { @@ -172,11 +188,7 @@ public class SignMojo extends AbstractMojo pgpSigner.sign( inputStream, target ); - return SignResult.builder() - .classifier( classifier ) - .extension( targetExt ) - .file( target.toFile() ) - .build(); + return new SignResult( classifier, targetExt, target.toFile() ); } /** diff --git a/src/main/java/org/apache/maven/plugins/sign/SignResult.java b/src/main/java/org/apache/maven/plugins/sign/SignMojoException.java similarity index 72% copy from src/main/java/org/apache/maven/plugins/sign/SignResult.java copy to src/main/java/org/apache/maven/plugins/sign/SignMojoException.java index 3cc2031..6cff61b 100644 --- a/src/main/java/org/apache/maven/plugins/sign/SignResult.java +++ b/src/main/java/org/apache/maven/plugins/sign/SignMojoException.java @@ -19,23 +19,19 @@ package org.apache.maven.plugins.sign; * under the License. */ -import lombok.Builder; -import lombok.ToString; -import lombok.Value; - -import java.io.File; - /** - * Result of signing artifact. + * Problems during mojo executing. + * <p> + * We extend from {@link RuntimeException} to easy use in Java stream process. * * @author Slawomir Jaranowski */ -@Value -@Builder -@ToString -public class SignResult +public class SignMojoException extends RuntimeException { - String classifier; - String extension; - File file; + private static final long serialVersionUID = -5595317162801384040L; + + public SignMojoException( Throwable cause ) + { + super( cause ); + } } diff --git a/src/main/java/org/apache/maven/plugins/sign/SignResult.java b/src/main/java/org/apache/maven/plugins/sign/SignResult.java index 3cc2031..929831e 100644 --- a/src/main/java/org/apache/maven/plugins/sign/SignResult.java +++ b/src/main/java/org/apache/maven/plugins/sign/SignResult.java @@ -19,10 +19,6 @@ package org.apache.maven.plugins.sign; * under the License. */ -import lombok.Builder; -import lombok.ToString; -import lombok.Value; - import java.io.File; /** @@ -30,12 +26,41 @@ import java.io.File; * * @author Slawomir Jaranowski */ -@Value -@Builder -@ToString public class SignResult { - String classifier; - String extension; - File file; + private final String classifier; + private final String extension; + private final File file; + + + public SignResult( String classifier, String extension, File file ) + { + this.classifier = classifier; + this.extension = extension; + this.file = file; + } + + public String getClassifier() + { + return classifier; + } + + public String getExtension() + { + return extension; + } + + public File getFile() + { + return file; + } + + @Override + public String toString() + { + return "SignResult[" + + "classifier=" + classifier + + ", extension=" + extension + + ", file=" + file + ']'; + } } diff --git a/src/main/java/org/apache/maven/plugins/sign/pgp/PGPSigner.java b/src/main/java/org/apache/maven/plugins/sign/pgp/PGPSigner.java index 6a874b4..26d998f 100644 --- a/src/main/java/org/apache/maven/plugins/sign/pgp/PGPSigner.java +++ b/src/main/java/org/apache/maven/plugins/sign/pgp/PGPSigner.java @@ -19,7 +19,6 @@ package org.apache.maven.plugins.sign.pgp; * under the License. */ -import lombok.extern.slf4j.Slf4j; import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.BCPGOutputStream; import org.bouncycastle.bcpg.HashAlgorithmTags; @@ -34,6 +33,8 @@ import org.bouncycastle.openpgp.PGPUtil; import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.FileInputStream; import java.io.IOException; @@ -50,10 +51,11 @@ import java.util.List; * * @author Slawomir Jaranowski */ -@Slf4j public class PGPSigner { + private static final Logger LOGGER = LoggerFactory.getLogger( PGPSigner.class ); + private final PGPSecretKeyInfo keyInfo; private PGPSecretKey secretKey; @@ -129,10 +131,12 @@ public class PGPSigner { sGen.init( PGPSignature.BINARY_DOCUMENT, pgpPrivateKey ); - int i; - while ( ( i = inputStream.read() ) >= 0 ) + + int len; + byte[] buffer = new byte[8 * 1024]; + while ( ( len = inputStream.read( buffer ) ) >= 0 ) { - sGen.update( (byte) i ); + sGen.update( buffer, 0, len ); } Files.createDirectories( outputPath.getParent() ); diff --git a/src/test/java/org/apache/maven/plugins/sign/PGPSecretKeyInfoFromSettingsTest.java b/src/test/java/org/apache/maven/plugins/sign/PGPSecretKeyInfoFromSettingsTest.java index c013703..1a221e5 100644 --- a/src/test/java/org/apache/maven/plugins/sign/PGPSecretKeyInfoFromSettingsTest.java +++ b/src/test/java/org/apache/maven/plugins/sign/PGPSecretKeyInfoFromSettingsTest.java @@ -37,8 +37,8 @@ import static org.mockito.Mockito.when; public class PGPSecretKeyInfoFromSettingsTest { - private static final String KEY_ID_HEX = "ABCDEF0123456789"; - private static final long KEY_ID = 0xABCDEF0123456789L; + private static final String[] KEY_ID_HEX = {"ABCDEF0123456789", "0000000000000000", "FFFFFFFFFFFFFFFF"}; + private static final long[] KEY_ID = {0xABCDEF0123456789L, 0L, 0xFFFFFFFFFFFFFFFFL}; private static final String KEY_FILE_STR = "test.asc"; private static final File KEY_FILE = new File( KEY_FILE_STR ); @@ -46,6 +46,7 @@ public class PGPSecretKeyInfoFromSettingsTest private static final String KEY_PASS_STR = "pass"; private static final char[] KEY_PASS = KEY_PASS_STR.toCharArray(); + @Mock private Server server; @@ -53,7 +54,7 @@ public class PGPSecretKeyInfoFromSettingsTest public void allProperties() { // given - when( server.getUsername() ).thenReturn( KEY_ID_HEX ); + when( server.getUsername() ).thenReturn( KEY_ID_HEX[0] ); when( server.getPrivateKey() ).thenReturn( KEY_FILE_STR ); when( server.getPassphrase() ).thenReturn( KEY_PASS_STR ); @@ -61,7 +62,7 @@ public class PGPSecretKeyInfoFromSettingsTest PGPSecretKeyInfoFromSettings info = new PGPSecretKeyInfoFromSettings( server ); // then - assertThat( info.getKeyId(), is( KEY_ID ) ); + assertThat( info.getKeyId(), is( KEY_ID[0] ) ); assertThat( info.getFile(), is( KEY_FILE ) ); assertThat( info.getPassphrase(), is( KEY_PASS ) ); } @@ -73,4 +74,20 @@ public class PGPSecretKeyInfoFromSettingsTest () -> new PGPSecretKeyInfoFromSettings( null ) ); } + @Test + public void keyIdShouldBeParsedProperly() + { + + for ( int i = 0; i < KEY_ID_HEX.length; i++ ) + { + // given + when( server.getUsername() ).thenReturn( KEY_ID_HEX[i] ); + + // when + PGPSecretKeyInfoFromSettings info = new PGPSecretKeyInfoFromSettings( server ); + + // then + assertThat( info.getKeyId(), is( KEY_ID[i] ) ); + } + } } diff --git a/src/test/java/org/apache/maven/plugins/sign/pgp/PGPSignerTest.java b/src/test/java/org/apache/maven/plugins/sign/pgp/PGPSignerTest.java index 7ffe2e2..b2c8823 100644 --- a/src/test/java/org/apache/maven/plugins/sign/pgp/PGPSignerTest.java +++ b/src/test/java/org/apache/maven/plugins/sign/pgp/PGPSignerTest.java @@ -19,21 +19,22 @@ package org.apache.maven.plugins.sign.pgp; * under the License. */ -import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; -@Slf4j @RunWith( MockitoJUnitRunner.class ) public class PGPSignerTest { + private static final Logger LOGGER = LoggerFactory.getLogger( PGPSignerTest.class ); @Mock private PGPSecretKeyInfo keyInfo;