This is an automated email from the ASF dual-hosted git repository. cstamas pushed a commit to branch get-rid-of-legacy in repository https://gitbox.apache.org/repos/asf/maven-remote-resources-plugin.git
commit 596e848cca405b1edfd66877e871c1f6263795f0 Author: Tamas Cservenak <[email protected]> AuthorDate: Mon Mar 6 15:18:16 2023 +0100 Get rid of legacy And with 3.9.0 suddenly all the ITs suffer from "change packaging to pom" error. --- pom.xml | 39 ++- .../remote/ProcessRemoteResourcesMojo.java | 264 ++++++---------- .../resources/remote/RemoteResourcesMojoTest.java | 344 +++++++++++---------- .../unit/rrmojotest/process-plugin-config.xml | 1 - 4 files changed, 288 insertions(+), 360 deletions(-) diff --git a/pom.xml b/pom.xml index fab26a9..f5b4a48 100644 --- a/pom.xml +++ b/pom.xml @@ -73,9 +73,19 @@ under the License. <project.build.outputTimestamp>2022-07-17T16:33:17Z</project.build.outputTimestamp> <!-- Used by site documentation as well, do not remove --> - <mavenFilteringVersion>3.2.0</mavenFilteringVersion> + <mavenFilteringVersion>3.3.0</mavenFilteringVersion> </properties> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + <version>3.5.1</version> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> <!-- maven core --> <dependency> @@ -119,47 +129,36 @@ under the License. <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-archiver</artifactId> - <version>3.5.1</version> + <version>3.6.0</version> </dependency> <dependency> <groupId>org.apache.maven.shared</groupId> - <artifactId>maven-artifact-transfer</artifactId> - <version>0.13.1</version> + <artifactId>maven-filtering</artifactId> + <version>${mavenFilteringVersion}</version> </dependency> <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-common-artifact-filters</artifactId> - <version>3.2.0</version> - </dependency> - <dependency> - <groupId>org.apache.maven.shared</groupId> - <artifactId>maven-filtering</artifactId> - <version>${mavenFilteringVersion}</version> + <version>3.3.2</version> </dependency> <!-- plexus --> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-resources</artifactId> - <version>1.1.0</version> - <exclusions> - <exclusion> - <groupId>org.codehaus.plexus</groupId> - <artifactId>plexus-container-default</artifactId> - </exclusion> - </exclusions> + <version>1.2.0</version> </dependency> <!-- other --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> - <version>2.6</version> + <version>2.11.0</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> - <artifactId>velocity</artifactId> - <version>1.7</version> + <artifactId>velocity-engine-core</artifactId> + <version>2.3</version> </dependency> <!-- test --> diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java index 4adbf31..571b1e5 100644 --- a/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java +++ b/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java @@ -56,12 +56,14 @@ import java.util.TreeMap; import org.apache.commons.io.output.DeferredFileOutputStream; import org.apache.maven.ProjectDependenciesResolver; +import org.apache.maven.RepositoryUtils; import org.apache.maven.archiver.MavenArchiver; +//import org.apache.maven.artifact.Artifact; +//import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.execution.MavenSession; import org.apache.maven.lifecycle.internal.ProjectArtifactFactory; @@ -85,7 +87,6 @@ import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingResult; import org.apache.maven.project.artifact.InvalidDependencyVersionException; -import org.apache.maven.repository.RepositorySystem; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter; import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; @@ -95,9 +96,6 @@ import org.apache.maven.shared.artifact.filter.collection.ScopeFilter; import org.apache.maven.shared.filtering.MavenFileFilter; import org.apache.maven.shared.filtering.MavenFileFilterRequest; import org.apache.maven.shared.filtering.MavenFilteringException; -import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver; -import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; -import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; @@ -105,9 +103,6 @@ import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.exception.VelocityException; -import org.apache.velocity.runtime.RuntimeConstants; -import org.apache.velocity.runtime.RuntimeServices; -import org.apache.velocity.runtime.log.LogChute; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; import org.codehaus.plexus.resource.ResourceManager; import org.codehaus.plexus.resource.loader.FileResourceLoader; @@ -118,6 +113,10 @@ import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.WriterFactory; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; /** * <p> @@ -139,7 +138,6 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @Mojo( name = "process", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true ) public class ProcessRemoteResourcesMojo extends AbstractMojo - implements LogChute { private static final String TEMPLATE_SUFFIX = ".vm"; @@ -195,24 +193,6 @@ public class ProcessRemoteResourcesMojo @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" ) protected String encoding; - /** - * 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; - - /** - * The current Maven project. - */ - @Parameter( defaultValue = "${project}", readonly = true, required = true ) - private MavenProject project; - /** * The directory where processed resources will be placed for packaging. */ @@ -246,17 +226,6 @@ public class ProcessRemoteResourcesMojo @Parameter private List<String> supplementalModelArtifacts; - /** - * Map of artifacts to supplemental project object models. - */ - private Map<String, Model> supplementModels; - - /** - * Merges supplemental data model with artifact metadata. Useful when processing artifacts with - * incomplete POM metadata. - */ - private ModelInheritanceAssembler inheritanceAssembler = new ModelInheritanceAssembler(); - /** * The resource bundles that will be retrieved and processed, * expressed with <code>groupId:artifactId:version[:type[:classifier]]</code> format. @@ -323,53 +292,12 @@ public class ProcessRemoteResourcesMojo @Parameter( defaultValue = "5242880" ) protected int velocityFilterInMemoryThreshold = 5 * 1024 * 1024; - /** - * The list of resources defined for the project. - */ - @Parameter( defaultValue = "${project.resources}", readonly = true, required = true ) - private List<Resource> resources; - - /** - * Repository system, needed to create Artifact and Repository objects. - */ - @Component - protected RepositorySystem repositorySystem; - - /** - * Artifact Resolver, needed to resolve and download the {@code resourceBundles}. - */ - @Component - private ArtifactResolver artifactResolver; - - /** - * Filtering support, for local resources that override those in the remote bundle. - */ - @Component - private MavenFileFilter fileFilter; - - /** - * Project artifact factory, needed to create artifacts for the project dependencies. - */ - @Component - private ProjectArtifactFactory projectArtifactFactory; - /** * The Maven session. */ @Parameter( defaultValue = "${session}", readonly = true, required = true ) private MavenSession mavenSession; - /** - * ProjectBuilder, needed to create projects from the artifacts. - */ - @Component( role = ProjectBuilder.class ) - private ProjectBuilder projectBuilder; - - /** - */ - @Component - private ResourceManager locator; - /** * Scope to include. An Empty string indicates all scopes (default is "runtime"). * @@ -391,14 +319,14 @@ public class ProcessRemoteResourcesMojo * The default is the same as "includeScope" if there are no exclude scopes set. * Otherwise, it defaults to "test" to grab all the dependencies so the * exclude filters can filter out what is not needed. - * + * * @since 1.5 */ @Parameter private String[] resolveScopes; /** - * Comma separated list of Artifact names too exclude. + * Comma separated list of Artifact names to exclude. * * @since 1.0 */ @@ -446,9 +374,46 @@ public class ProcessRemoteResourcesMojo private String outputTimestamp; /** + * Repository system, needed to create Artifact and Repository objects. */ - @Component( hint = "default" ) - protected ProjectDependenciesResolver dependencyResolver; + @Component + protected RepositorySystem repositorySystem; + + /** + * Filtering support, for local resources that override those in the remote bundle. + */ + @Component + private MavenFileFilter fileFilter; + + /** + */ + @Component + private ResourceManager locator; + + @Component + private ProjectBuilder projectBuilder; + + @Component + private ArtifactHandlerManager artifactHandlerManager; + + //TODO: move off from this component + @Component + private ProjectDependenciesResolver projectDependenciesResolver; + + //TODO: move off from this component + @Component + private ProjectArtifactFactory projectArtifactFactory; + + /** + * Map of artifacts to supplemental project object models. + */ + private Map<String, Model> supplementModels; + + /** + * Merges supplemental data model with artifact metadata. Useful when processing artifacts with + * incomplete POM metadata. + */ + private final ModelInheritanceAssembler inheritanceAssembler = new ModelInheritanceAssembler(); private VelocityEngine velocity; @@ -469,7 +434,7 @@ public class ProcessRemoteResourcesMojo + ", i.e. build is platform dependent!" ); } - if ( runOnlyAtExecutionRoot && !project.isExecutionRoot() ) + if ( runOnlyAtExecutionRoot && !mavenSession.getCurrentProject().isExecutionRoot() ) { getLog().info( "Skipping remote-resource generation in this project because it's not the Execution Root" ); return; @@ -502,7 +467,7 @@ public class ProcessRemoteResourcesMojo if ( includeProjectProperties ) { - final Properties projectProperties = project.getProperties(); + final Properties projectProperties = mavenSession.getCurrentProject().getProperties(); for ( Object key : projectProperties.keySet() ) { properties.put( key.toString(), projectProperties.get( key ).toString() ); @@ -522,7 +487,6 @@ public class ProcessRemoteResourcesMojo Thread.currentThread().setContextClassLoader( classLoader ); velocity = new VelocityEngine(); - velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this ); velocity.setProperty( "resource.loader", "classpath" ); velocity.setProperty( "classpath.resource.loader.class", ClasspathResourceLoader.class.getName() ); velocity.init(); @@ -542,11 +506,11 @@ public class ProcessRemoteResourcesMojo // MRRESOURCES-61 handle main and test resources separately if ( attachToMain ) { - project.getResources().add( resource ); + mavenSession.getCurrentProject().getResources().add( resource ); } if ( attachToTest ) { - project.getTestResources().add( resource ); + mavenSession.getCurrentProject().getTestResources().add( resource ); } // ---------------------------------------------------------------------------- @@ -554,7 +518,7 @@ public class ProcessRemoteResourcesMojo // ---------------------------------------------------------------------------- try { - File dotFile = new File( project.getBuild().getDirectory(), ".plxarc" ); + File dotFile = new File( mavenSession.getCurrentProject().getBuild().getDirectory(), ".plxarc" ); FileUtils.mkdir( dotFile.getParentFile().getAbsolutePath() ); FileUtils.fileWrite( dotFile.getAbsolutePath(), outputDirectory.getName() ); } @@ -598,13 +562,14 @@ public class ProcessRemoteResourcesMojo } - locator.addSearchPath( FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath() ); + locator.addSearchPath( FileResourceLoader.ID, mavenSession.getCurrentProject() + .getFile().getParentFile().getAbsolutePath() ); if ( appendedResourcesDirectory != null ) { locator.addSearchPath( FileResourceLoader.ID, appendedResourcesDirectory.getAbsolutePath() ); } locator.addSearchPath( "url", "" ); - locator.setOutputDirectory( new File( project.getBuild().getDirectory() ) ); + locator.setOutputDirectory( new File( mavenSession.getCurrentProject().getBuild().getDirectory() ) ); } @SuppressWarnings( "unchecked" ) @@ -625,7 +590,7 @@ public class ProcessRemoteResourcesMojo } else { - depArtifacts = project.getDependencyArtifacts(); + depArtifacts = mavenSession.getCurrentProject().getDependencyArtifacts(); } filter.addFilter( new ProjectTransitivityFilter( depArtifacts, true ) ); } @@ -646,7 +611,7 @@ public class ProcessRemoteResourcesMojo for ( Artifact artifact : artifacts ) { - List<ArtifactRepository> remoteRepo = remoteArtifactRepositories; + List<ArtifactRepository> remoteRepo = mavenSession.getCurrentProject().getRemoteArtifactRepositories(); if ( artifact.isSnapshot() ) { VersionRange rng = VersionRange.createFromVersion( artifact.getBaseVersion() ); @@ -665,7 +630,7 @@ public class ProcessRemoteResourcesMojo .setRepositorySession( mavenSession.getRepositorySession() ) .setSystemProperties( mavenSession.getSystemProperties() ) .setUserProperties( mavenSession.getUserProperties() ) - .setLocalRepository( localRepository ) + .setLocalRepository( mavenSession.getLocalRepository() ) .setRemoteRepositories( remoteRepo ); ProjectBuildingResult res = projectBuilder.build( artifact, req ); p = res.getProject(); @@ -696,7 +661,7 @@ public class ProcessRemoteResourcesMojo getLog().debug( "Adding project with groupId [" + p.getGroupId() + "]" ); } } - projects.sort( new ProjectComparator( ) ); + projects.sort( new ProjectComparator() ); return projects; } @@ -707,21 +672,21 @@ public class ProcessRemoteResourcesMojo if ( runOnlyAtExecutionRoot ) { List<MavenProject> projects = mavenSession.getProjects(); - return dependencyResolver.resolve( projects, Arrays.asList( resolveScopes ), mavenSession ); + return projectDependenciesResolver.resolve( projects, Arrays.asList( resolveScopes ), mavenSession ); } else { - return dependencyResolver.resolve( project, Arrays.asList( resolveScopes ), mavenSession ); + return projectDependenciesResolver.resolve( mavenSession.getCurrentProject(), + Arrays.asList( resolveScopes ), mavenSession ); } } - catch ( ArtifactResolutionException | ArtifactNotFoundException e ) + catch ( Exception e ) { throw new IllegalStateException( "Failed to resolve dependencies for one or more projects in the reactor. " + "Reason: " + e.getMessage(), e ); } } - @SuppressWarnings( "unchecked" ) private Set<Artifact> aggregateProjectDependencyArtifacts() { Set<Artifact> artifacts = new LinkedHashSet<>(); @@ -799,7 +764,7 @@ public class ProcessRemoteResourcesMojo protected boolean copyResourceIfExists( File file, String relFileName, VelocityContext context ) throws IOException, MojoExecutionException { - for ( Resource resource : resources ) + for ( Resource resource : mavenSession.getCurrentProject().getResources() ) { File resourceDirectory = new File( resource.getDirectory() ); @@ -945,7 +910,7 @@ public class ProcessRemoteResourcesMojo req.setTo( file ); req.setFiltering( resource.isFiltering() ); - req.setMavenProject( project ); + req.setMavenProject( mavenSession.getCurrentProject() ); req.setMavenSession( mavenSession ); req.setInjectProjectBuildFilters( true ); @@ -1058,7 +1023,7 @@ public class ProcessRemoteResourcesMojo MavenArchiver archiver = new MavenArchiver(); Date outputDate = archiver.parseOutputTimestamp( outputTimestamp ); - String inceptionYear = project.getInceptionYear(); + String inceptionYear = mavenSession.getCurrentProject().getInceptionYear(); String year = new SimpleDateFormat( "yyyy" ).format( ( outputDate == null ) ? new Date() : outputDate ); if ( StringUtils.isEmpty( inceptionYear ) ) @@ -1070,7 +1035,7 @@ public class ProcessRemoteResourcesMojo inceptionYear = year; } - context.put( "project", project ); + context.put( "project", mavenSession.getCurrentProject() ); context.put( "presentYear", year ); context.put( "locator", locator ); @@ -1119,17 +1084,28 @@ public class ProcessRemoteResourcesMojo } if ( artifactFile == null || !artifactFile.exists() ) { + String g = s[0]; + String a = s[1]; + String v = s[2]; String type = ( s.length >= 4 ? s[3] : "jar" ); String classifier = ( s.length == 5 ? s[4] : null ); - Artifact artifact = repositorySystem.createArtifactWithClassifier( s[0], s[1], s[2], type, classifier ); + + VersionRange rng = VersionRange.createFromVersion( v ); + Artifact artifact = new DefaultArtifact( g, a, rng, + Artifact.SCOPE_RUNTIME, type, classifier, + artifactHandlerManager.getArtifactHandler( type ), false ); try { - ArtifactResult result = artifactResolver.resolveArtifact( - mavenSession.getProjectBuildingRequest(), artifact ); + ArtifactRequest request = new ArtifactRequest( + RepositoryUtils.toArtifact( artifact ), + RepositoryUtils.toRepos( mavenSession.getCurrentProject().getRemoteArtifactRepositories() ), + "remote-resources" ); + ArtifactResult result = repositorySystem.resolveArtifact( + mavenSession.getRepositorySession(), request ); artifactFile = result.getArtifact().getFile(); } - catch ( ArtifactResolverException e ) + catch ( ArtifactResolutionException e ) { throw new MojoExecutionException( "Error processing remote resources", e ); } @@ -1192,8 +1168,7 @@ public class ProcessRemoteResourcesMojo n++; resourceCount++; getLog().debug( "bundle#" + bundleCount + " resource#" + n + " " + bundleResource ); - remoteResources.add( new AbstractMap.SimpleEntry<>( bundleResource, - bundle ) ); + remoteResources.add( new AbstractMap.SimpleEntry<>( bundleResource, bundle ) ); } } } @@ -1445,7 +1420,7 @@ public class ProcessRemoteResourcesMojo return loc; } - class OrganizationComparator + static class OrganizationComparator implements Comparator<Organization> { @Override @@ -1470,11 +1445,11 @@ public class ProcessRemoteResourcesMojo { return 0; } - else if ( s1 == null && s2 != null ) + else if ( s1 == null ) { return 1; } - else if ( s1 != null && s2 == null ) + else if ( s2 == null ) { return -1; } @@ -1483,7 +1458,7 @@ public class ProcessRemoteResourcesMojo } } - class ProjectComparator + static class ProjectComparator implements Comparator<MavenProject> { @Override @@ -1498,65 +1473,4 @@ public class ProcessRemoteResourcesMojo } } - /* LogChute methods */ - @Override - public void init( RuntimeServices rs ) - throws Exception - { - } - - @Override - public void log( int level, String message ) - { - switch ( level ) - { - case LogChute.WARN_ID: - getLog().warn( message ); - break; - case LogChute.INFO_ID: - // velocity info messages are too verbose, just consider them as debug messages... - getLog().debug( message ); - break; - case LogChute.DEBUG_ID: - getLog().debug( message ); - break; - case LogChute.ERROR_ID: - getLog().error( message ); - break; - default: - getLog().debug( message ); - break; - } - } - - @Override - public void log( int level, String message, Throwable t ) - { - switch ( level ) - { - case LogChute.WARN_ID: - getLog().warn( message, t ); - break; - case LogChute.INFO_ID: - // velocity info messages are too verbose, just consider them as debug messages... - getLog().debug( message, t ); - break; - case LogChute.DEBUG_ID: - getLog().debug( message, t ); - break; - case LogChute.ERROR_ID: - getLog().error( message, t ); - break; - default: - getLog().debug( message, t ); - break; - } - } - - @Override - public boolean isLevelEnabled( int level ) - { - return false; - } - } diff --git a/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java b/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java index 26e8e0f..93116cf 100644 --- a/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java +++ b/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java @@ -19,20 +19,6 @@ package org.apache.maven.plugin.resources.remote; * under the License. */ -import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.artifact.handler.DefaultArtifactHandler; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.versioning.VersionRange; -import org.apache.maven.execution.DefaultMavenExecutionRequest; -import org.apache.maven.execution.DefaultMavenExecutionResult; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.resources.remote.stub.MavenProjectBuildStub; -import org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.project.MavenProject; -import org.apache.maven.repository.internal.MavenRepositorySystemUtils; -import org.codehaus.plexus.util.FileUtils; - import java.io.File; import java.io.InputStream; import java.io.OutputStream; @@ -45,6 +31,23 @@ import java.util.Calendar; import java.util.Collections; import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.artifact.repository.MavenArtifactRepository; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.resources.remote.stub.MavenProjectBuildStub; +import org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.LocalArtifactRepository; +import org.apache.maven.repository.internal.MavenRepositorySystemUtils; +import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; @@ -55,7 +58,7 @@ import org.eclipse.aether.repository.LocalRepository; * RemoteResources plugin Test Case */ public class RemoteResourcesMojoTest - extends AbstractMojoTestCase + extends AbstractMojoTestCase { static final String DEFAULT_BUNDLE_POM_PATH = "target/test-classes/unit/rrmojotest/bundle-plugin-config.xml"; static final String DEFAULT_PROCESS_POM_PATH = "target/test-classes/unit/rrmojotest/process-plugin-config.xml"; @@ -64,14 +67,14 @@ public class RemoteResourcesMojoTest @Override public void setUp() - throws Exception + throws Exception { super.setUp(); } @Override public void tearDown() - throws Exception + throws Exception { } @@ -82,7 +85,7 @@ public class RemoteResourcesMojoTest * @throws Exception if any exception occurs */ public void testTestEnvironment() - throws Exception + throws Exception { // Perform lookup on the Mojo to make sure everything is ok lookupProcessMojo(); @@ -90,7 +93,7 @@ public class RemoteResourcesMojoTest public void testNoBundles() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-nobundles" ); final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithDefaultSettings( project ); @@ -101,40 +104,39 @@ public class RemoteResourcesMojoTest } public void testCreateBundle() - throws Exception + throws Exception { buildResourceBundle( "default-createbundle", - null, - new String[] { "SIMPLE.txt" }, - null ); + null, + new String[] {"SIMPLE.txt"}, + null ); } public void testSimpleBundles() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-simplebundles" ); - final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project , - new String[] { - "test:test:1.0" - } ); + final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project, + new String[] { + "test:test:1.0" + } ); setupDefaultProject( project ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test", - "test", - VersionRange.createFromVersion( "1.0" ), - null, - "jar", - "", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test", + "test", + VersionRange.createFromVersion( "1.0" ), + null, + "jar", + "", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".jar" ); + File file = new File( path ); file.getParentFile().mkdirs(); buildResourceBundle( "default-simplebundles-create", - null, - new String[] { "SIMPLE.txt" }, - file ); + null, + new String[] {"SIMPLE.txt"}, + file ); mojo.execute(); @@ -145,31 +147,30 @@ public class RemoteResourcesMojoTest } public void testSimpleBundlesWithType() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-simplebundles" ); - final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project , - new String[] { - "test:test:1.0:war" - } ); + final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project, + new String[] { + "test:test:1.0:war" + } ); setupDefaultProject( project ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test", - "test", - VersionRange.createFromVersion( "1.0" ), - null, - "war", - "", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test", + "test", + VersionRange.createFromVersion( "1.0" ), + null, + "war", + "", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".war" ); + File file = new File( path ); file.getParentFile().mkdirs(); buildResourceBundle( "default-simplebundles-create", - null, - new String[] { "SIMPLE.txt" }, - file ); + null, + new String[] {"SIMPLE.txt"}, + file ); mojo.execute(); @@ -180,31 +181,30 @@ public class RemoteResourcesMojoTest } public void testSimpleBundlesWithClassifier() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-simplebundles" ); - final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project , - new String[] { - "test:test:1.0:jar:test" - } ); + final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project, + new String[] { + "test:test:1.0:jar:test" + } ); setupDefaultProject( project ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test", - "test", - VersionRange.createFromVersion( "1.0" ), - null, - "jar", - "test", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test", + "test", + VersionRange.createFromVersion( "1.0" ), + null, + "jar", + "test", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".jar" ); + File file = new File( path ); file.getParentFile().mkdirs(); buildResourceBundle( "default-simplebundles-create", - null, - new String[] { "SIMPLE.txt" }, - file ); + null, + new String[] {"SIMPLE.txt"}, + file ); mojo.execute(); @@ -215,31 +215,30 @@ public class RemoteResourcesMojoTest } public void testVelocityUTF8() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-utf8" ); - final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project , - new String[] { - "test:test:1.2" - } ); + final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project, + new String[] { + "test:test:1.2" + } ); setupDefaultProject( project ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test", - "test", - VersionRange.createFromVersion( "1.2" ), - null, - "jar", - "", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test", + "test", + VersionRange.createFromVersion( "1.2" ), + null, + "jar", + "", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".jar" ); + File file = new File( path ); file.getParentFile().mkdirs(); buildResourceBundle( "default-utf8-create", - "UTF-8", - new String[] { "UTF-8.bin.vm" }, - file ); + "UTF-8", + new String[] {"UTF-8.bin.vm"}, + file ); mojo.execute(); @@ -247,7 +246,7 @@ public class RemoteResourcesMojoTest file = new File( file, "UTF-8.bin" ); assertTrue( file.exists() ); - try (InputStream in = Files.newInputStream( file.toPath( ) ) ) + try ( InputStream in = Files.newInputStream( file.toPath() ) ) { byte[] data = IOUtil.toByteArray( in ); byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes( StandardCharsets.UTF_8 ); @@ -256,31 +255,30 @@ public class RemoteResourcesMojoTest } public void testVelocityISO88591() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-iso88591" ); - final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project , - new String[] { - "test:test:1.3" - } ); + final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project, + new String[] { + "test:test:1.3" + } ); setupDefaultProject( project ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test", - "test", - VersionRange.createFromVersion( "1.3" ), - null, - "jar", - "", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test", + "test", + VersionRange.createFromVersion( "1.3" ), + null, + "jar", + "", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".jar" ); + File file = new File( path ); file.getParentFile().mkdirs(); buildResourceBundle( "default-iso88591-create", - "ISO-8859-1", - new String[] { "ISO-8859-1.bin.vm" }, - file ); + "ISO-8859-1", + new String[] {"ISO-8859-1.bin.vm"}, + file ); mojo.execute(); @@ -288,7 +286,7 @@ public class RemoteResourcesMojoTest file = new File( file, "ISO-8859-1.bin" ); assertTrue( file.exists() ); - try (InputStream in = Files.newInputStream( file.toPath() ) ) + try ( InputStream in = Files.newInputStream( file.toPath() ) ) { byte[] data = IOUtil.toByteArray( in ); byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes( StandardCharsets.ISO_8859_1 ); @@ -298,31 +296,30 @@ public class RemoteResourcesMojoTest } public void testFilteredBundles() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-filterbundles" ); - final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project , - new String[] { - "test:test:1.1" - } ); + final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project, + new String[] { + "test:test:1.1" + } ); setupDefaultProject( project ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test", - "test", - VersionRange.createFromVersion( "1.1" ), - null, - "jar", - "", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test", + "test", + VersionRange.createFromVersion( "1.1" ), + null, + "jar", + "", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".jar" ); + File file = new File( path ); file.getParentFile().mkdirs(); buildResourceBundle( "default-filterbundles-create", - null, - new String[] { "FILTER.txt.vm" }, - file ); + null, + new String[] {"FILTER.txt.vm"}, + file ); mojo.execute(); @@ -341,11 +338,12 @@ public class RemoteResourcesMojoTest } public void testFilteredBundlesWithProjectProperties() - throws Exception + throws Exception { final MavenProjectResourcesStub project = createTestProject( "default-filterbundles-two" ); final ProcessRemoteResourcesMojo mojo = - lookupProcessMojoWithSettings( project, new String[]{"test-filtered-bundles:test-filtered-bundles:2"} ); + lookupProcessMojoWithSettings( project, + new String[] {"test-filtered-bundles:test-filtered-bundles:2"} ); mojo.includeProjectProperties = true; setupDefaultProject( project ); @@ -353,14 +351,13 @@ public class RemoteResourcesMojoTest project.addProperty( "testingPropertyOne", "maven" ); project.addProperty( "testingPropertyTwo", "rules" ); - ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" ); - String path = repo.pathOf( new DefaultArtifact( "test-filtered-bundles", "test-filtered-bundles", - VersionRange.createFromVersion( "2" ), null, "jar", "", - new DefaultArtifactHandler() ) ); + String path = pathOf( new DefaultArtifact( "test-filtered-bundles", "test-filtered-bundles", + VersionRange.createFromVersion( "2" ), null, "jar", "", + new DefaultArtifactHandler() ) ); - File file = new File( repo.getBasedir() + "/" + path + ".jar" ); + File file = new File( path ); file.getParentFile().mkdirs(); - buildResourceBundle( "default-filterbundles-two-create", null, new String[]{"PROPERTIES.txt.vm"}, file ); + buildResourceBundle( "default-filterbundles-two-create", null, new String[] {"PROPERTIES.txt.vm"}, file ); mojo.execute(); // executing a second time (example: forked lifecycle) should still work @@ -372,20 +369,20 @@ public class RemoteResourcesMojoTest assertTrue( file.exists() ); String data = FileUtils.fileRead( file ); - assertTrue(data.contains("maven")); - assertTrue(data.contains("rules")); + assertTrue( data.contains( "maven" ) ); + assertTrue( data.contains( "rules" ) ); } protected void buildResourceBundle( String id, - String sourceEncoding, - String[] resourceNames, - File jarName ) - throws Exception + String sourceEncoding, + String[] resourceNames, + File jarName ) + throws Exception { final MavenProjectResourcesStub project = createTestProject( id ); final File resourceDir = new File( project.getBasedir() + "/src/main/resources" ); - final BundleRemoteResourcesMojo mojo = lookupBundleMojoWithSettings( project , resourceDir, sourceEncoding ); + final BundleRemoteResourcesMojo mojo = lookupBundleMojoWithSettings( project, resourceDir, sourceEncoding ); setupDefaultProject( project ); @@ -394,7 +391,7 @@ public class RemoteResourcesMojoTest File resource = new File( resourceDir, resourceName2 ); URL source = getClass().getResource( "/" + resourceName2 ); - FileUtils.copyURLToFile(source, resource); + FileUtils.copyURLToFile( source, resource ); } mojo.execute(); @@ -432,9 +429,8 @@ public class RemoteResourcesMojoTest } - protected MavenProjectResourcesStub createTestProject( final String testName ) - throws Exception + throws Exception { // this will automatically create the isolated // test environment @@ -442,7 +438,7 @@ public class RemoteResourcesMojoTest } protected void setupDefaultProject( final MavenProjectResourcesStub project ) - throws Exception + throws Exception { // put this on the root dir project.addFile( "pom.xml", MavenProjectBuildStub.ROOT_FILE ); @@ -453,7 +449,7 @@ public class RemoteResourcesMojoTest protected BundleRemoteResourcesMojo lookupBundleMojo() - throws Exception + throws Exception { File pomFile = new File( getBasedir(), DEFAULT_BUNDLE_POM_PATH ); BundleRemoteResourcesMojo mojo = (BundleRemoteResourcesMojo) lookupMojo( "bundle", pomFile ); @@ -464,7 +460,7 @@ public class RemoteResourcesMojoTest } protected BundleRemoteResourcesMojo lookupBundleMojoWithDefaultSettings( final MavenProject project ) - throws Exception + throws Exception { File resourceDir = new File( project.getBasedir() + "/src/main/resources" ); return lookupBundleMojoWithSettings( project, resourceDir, null ); @@ -472,7 +468,7 @@ public class RemoteResourcesMojoTest protected BundleRemoteResourcesMojo lookupBundleMojoWithSettings( final MavenProject project, File resourceDir, String sourceEncoding ) - throws Exception + throws Exception { final BundleRemoteResourcesMojo mojo = lookupBundleMojo(); @@ -483,7 +479,7 @@ public class RemoteResourcesMojoTest } protected ProcessRemoteResourcesMojo lookupProcessMojo() - throws Exception + throws Exception { File pomFile = new File( getBasedir(), DEFAULT_PROCESS_POM_PATH ); ProcessRemoteResourcesMojo mojo = (ProcessRemoteResourcesMojo) lookupMojo( "process", pomFile ); @@ -495,46 +491,66 @@ public class RemoteResourcesMojoTest protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project, - String[] bundles ) - throws Exception + String[] bundles ) + throws Exception { return lookupProcessMojoWithSettings( project, new ArrayList<>( Arrays.asList( bundles ) ) ); } protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project, - ArrayList<String> bundles ) - throws Exception + ArrayList<String> bundles ) + throws Exception { final ProcessRemoteResourcesMojo mojo = lookupProcessMojo(); - ArtifactRepository localRepository = mojo.repositorySystem.createLocalRepository( new File( LOCAL_REPO ) ); DefaultRepositorySystemSession reposession = MavenRepositorySystemUtils.newSession(); reposession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory() .newInstance( reposession, new LocalRepository( new File( LOCAL_REPO ) ) ) ); DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest(); - request.setLocalRepository( localRepository ); - request = new DefaultMavenExecutionRequest(); request.setUserProperties( null ); - request.setLocalRepository( localRepository ); - request.setGoals(Collections.singletonList( "install" ) ); + //request.setLocalRepository( reposession.getLocalRepository() ); + request.setGoals( Collections.singletonList( "install" ) ); request.setBaseDirectory( project.getBasedir() ); request.setStartTime( Calendar.getInstance().getTime() ); - MavenSession session = new MavenSession( getContainer(), reposession, request, new DefaultMavenExecutionResult() ); + + // TODO: get rid of this uglyness/legacy + MavenArtifactRepository localRepository = new MavenArtifactRepository(); + localRepository.setId( "local" ); + localRepository.setUrl( new File( LOCAL_REPO ).toURI().toASCIIString() ); + localRepository.setLayout( new DefaultRepositoryLayout() ); + request.setLocalRepository( localRepository ); + MavenSession session = + new MavenSession( getContainer(), reposession, request, new DefaultMavenExecutionResult() ); session.setProjects( Collections.singletonList( project ) ); - setVariableValueToObject( mojo, "project", project ); setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) ); setVariableValueToObject( mojo, "resourceBundles", bundles ); setVariableValueToObject( mojo, "mavenSession", session ); - setVariableValueToObject( mojo, "remoteArtifactRepositories", project.getRemoteArtifactRepositories() ); - setVariableValueToObject( mojo, "resources", project.getResources() ); return mojo; } protected ProcessRemoteResourcesMojo lookupProcessMojoWithDefaultSettings( final MavenProject project ) - throws Exception + throws Exception { return lookupProcessMojoWithSettings( project, new ArrayList<>() ); } + + private String pathOf( Artifact artifact ) + { + String path = LOCAL_REPO + artifact.getGroupId().replaceAll( "\\.", "/" ) + "/" + + artifact.getArtifactId() + "/" + + artifact.getBaseVersion() + "/" + + artifact.getArtifactId() + "-" + artifact.getVersion(); + if ( artifact.getClassifier() != null && !artifact.getClassifier().isEmpty() ) + { + path = path + "-" + artifact.getClassifier(); + } + String ext = artifact.getArtifactHandler().getExtension(); + if ( ext == null ) { + ext = artifact.getType(); + } + path = path + "." + ext; + return path; + } } diff --git a/src/test/resources/unit/rrmojotest/process-plugin-config.xml b/src/test/resources/unit/rrmojotest/process-plugin-config.xml index 0f97b40..9d90b78 100644 --- a/src/test/resources/unit/rrmojotest/process-plugin-config.xml +++ b/src/test/resources/unit/rrmojotest/process-plugin-config.xml @@ -24,7 +24,6 @@ under the License. <plugin> <artifactId>maven-remote-resources-plugin</artifactId> <configuration> - <localRepository>${localRepository}</localRepository> <encoding>UTF-8</encoding> <outputTimestamp>2019-12-31T12:00:00Z</outputTimestamp> </configuration>
