This is an automated email from the ASF dual-hosted git repository. hboutemy pushed a commit to branch maven-buildinfo-plugin in repository https://gitbox.apache.org/repos/asf/maven-studies.git
commit 4522f560e24dd4dc88718533be286a0974324246 Author: Hervé Boutemy <hbout...@apache.org> AuthorDate: Sun Mar 1 18:04:24 2020 +0100 download reference buildinfo file --- pom.xml | 12 +- .../apache/maven/plugins/buildinfo/SaveMojo.java | 127 +++++++++++++++++++-- 2 files changed, 124 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 0b03588..d827f5e 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,12 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-compat</artifactId> + <version>${mavenVersion}</version> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>3.3.0</version> @@ -103,12 +109,6 @@ <version>1.13</version> </dependency> <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-compat</artifactId> - <version>${mavenVersion}</version> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <scope>provided</scope> diff --git a/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java b/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java index 5bbfa77..71056e1 100644 --- a/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java +++ b/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java @@ -20,6 +20,15 @@ package org.apache.maven.plugins.buildinfo; */ import org.apache.commons.codec.Charsets; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.artifact.repository.MavenArtifactRepository; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -29,6 +38,7 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; +import org.codehaus.plexus.util.FileUtils; import java.io.BufferedWriter; import java.io.File; @@ -36,6 +46,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.util.Collections; import java.util.List; /** @@ -79,10 +90,17 @@ public class SaveMojo //private String rebuildArgs; /** - * URL for reference build, containing either reference buildinfo file or reference artifacts. + * Repository for reference build, containing either reference buildinfo file or reference artifacts.<br/> + * Format: <code>id</code> or <code>url</code> or <code>id::url</code> + * <dl> + * <dt>id</dt> + * <dd>The id can be used to pick up the correct credentials from the settings.xml</dd> + * <dt>url</dt> + * <dd>The location of the repository</dd> + * </dl> */ - @Parameter( property = "reference.url" ) - private String referenceUrl; + @Parameter( property = "reference.repo" ) + private String referenceRepo; /** * Directory of the downloaded reference files. @@ -91,11 +109,32 @@ public class SaveMojo private File referenceDir; /** + * The local repository taken from Maven's runtime. Typically <code>$HOME/.m2/repository</code>. + */ + @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) + private ArtifactRepository localRepository; + + /** + * List of Remote Repositories used by the resolver. + */ + @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true ) + private List<ArtifactRepository> remoteArtifactRepositories; + + /** * Used for attaching the buildinfo file in the project. */ @Component private MavenProjectHelper projectHelper; + /** + * Artifact Resolver, needed to resolve and download the {@code resourceBundles}. + */ + @Component + private ArtifactResolver artifactResolver; + + @Component + private ArtifactFactory artifactFactory; + public void execute() throws MojoExecutionException { @@ -124,10 +163,12 @@ public class SaveMojo getLog().info( "NOT adding buildinfo to the list of attached artifacts." ); } - if ( referenceUrl != null ) + if ( referenceRepo != null ) { - getLog().info( "Checking against reference build from " + referenceUrl ); - checkAgainstReference(); + ArtifactRepository repo = createReferenceRepo(); + + getLog().info( "Checking against reference build from " + referenceRepo + "..." ); + checkAgainstReference( repo ); } } @@ -176,10 +217,78 @@ public class SaveMojo return null; } - private void checkAgainstReference() + private void checkAgainstReference( ArtifactRepository repo ) + throws MojoExecutionException { referenceDir.mkdirs(); - getLog().warn( "TODO try to download reference buildinfo and compare..." ); - getLog().warn( "TODO if no reference buildinfo, try to download reference artifacts and compare..." ); + + File referenceBuildinfo = downloadReferenceBuildinfo( repo ); + } + + private File downloadReferenceBuildinfo( ArtifactRepository repo ) + throws MojoExecutionException + { + File referenceBuildinfo = new File( referenceDir, buildinfoFile.getName() ); + + Artifact buildinfo = + artifactFactory.createArtifactWithClassifier( project.getGroupId(), project.getArtifactId(), + project.getVersion(), "buildinfo", "" ); + try + { + artifactResolver.resolve( buildinfo, Collections.singletonList( repo ), localRepository ); + + FileUtils.copyFile( buildinfo.getFile(), referenceBuildinfo ); + getLog().info( "Reference buildinfo file found, copied to " + referenceBuildinfo ); + } + catch ( ArtifactResolutionException are ) + { + throw new MojoExecutionException( "Error resolving buildinfo artifact " + buildinfo, are ); + } + catch ( ArtifactNotFoundException e ) + { + getLog().warn( "Reference buildinfo file not found: " + + "it will be generated from downloaded reference artifacts" ); + } + catch ( IOException ioe ) + { + throw new MojoExecutionException( "Error copying buildinfo artifact " + buildinfo, ioe ); + } + + return referenceBuildinfo; + } + + private ArtifactRepository createReferenceRepo() + throws MojoExecutionException + { + if ( referenceRepo.contains( "::" ) ) + { + // id::url + int index = referenceRepo.indexOf( "::" ); + String id = referenceRepo.substring( 0, index ); + String url = referenceRepo.substring( index + 2 ); + return createDeploymentArtifactRepository( id, url ); + } + else if ( referenceRepo.contains( ":" ) ) + { + // url, will use default "reference" id + return createDeploymentArtifactRepository( "reference", referenceRepo ); + } + + // id + for ( ArtifactRepository repo : remoteArtifactRepositories ) + { + if ( referenceRepo.equals( repo.getId() ) ) + { + return repo; + } + } + throw new MojoExecutionException( "Could not find repository with id = " + referenceRepo ); + } + + protected ArtifactRepository createDeploymentArtifactRepository( String id, String url ) + { + return new MavenArtifactRepository( id, url, new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), + new ArtifactRepositoryPolicy() ); } + }