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 980b2746c8fe1db6a3574b86020d80a6feb8de59 Author: Slawomir Jaranowski <s.jaranow...@gmail.com> AuthorDate: Sun Sep 27 12:26:09 2020 +0200 Try access to FileTransformer --- pom.xml | 6 +- src/it/standard-packaging/pom.xml | 16 +++ .../org/apache/maven/plugins/sign/SignMojo.java | 108 ++++++++++++++++++++- 3 files changed, 126 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 84d8b26..0f4885e 100644 --- a/pom.xml +++ b/pom.xml @@ -56,7 +56,7 @@ </distributionManagement> <properties> - <mavenVersion>3.0</mavenVersion> + <mavenVersion>3.6.0</mavenVersion> <javaVersion>8</javaVersion> </properties> @@ -84,6 +84,7 @@ <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> + <scope>provided</scope> </dependency> <dependency> @@ -107,9 +108,10 @@ <goals> <goal>install</goal> </goals> - <debug>false</debug> + <debug>true</debug> <showErrors>true</showErrors> <showVersion>true</showVersion> + <streamLogs>true</streamLogs> </configuration> </plugin> </plugins> diff --git a/src/it/standard-packaging/pom.xml b/src/it/standard-packaging/pom.xml index 627c7b9..e4cda5c 100644 --- a/src/it/standard-packaging/pom.xml +++ b/src/it/standard-packaging/pom.xml @@ -35,11 +35,27 @@ <artifactId>maven-sign-plugin</artifactId> <version>@project.version@</version> <executions> + + <execution> + <id>with-reflection</id> + <goals> + <goal>sign</goal> + </goals> + <configuration> + <useReflection>true</useReflection> + </configuration> + </execution> + <execution> + <id>with-method-call</id> <goals> <goal>sign</goal> </goals> + <configuration> + <useReflection>false</useReflection> + </configuration> </execution> + </executions> </plugin> </plugins> 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 f356273..19dcf43 100644 --- a/src/main/java/org/apache/maven/plugins/sign/SignMojo.java +++ b/src/main/java/org/apache/maven/plugins/sign/SignMojo.java @@ -20,11 +20,27 @@ package org.apache.maven.plugins.sign; */ import lombok.extern.slf4j.Slf4j; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; 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.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.transform.FileTransformer; +import org.eclipse.aether.transform.FileTransformerManager; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.stream.Collectors; /** * Sign project artifacts. @@ -37,10 +53,98 @@ public class SignMojo extends AbstractMojo @Parameter( defaultValue = "${project}", readonly = true, required = true ) private MavenProject project; + @Parameter( defaultValue = "${session}", required = true, readonly = true ) + protected MavenSession session; + + @Parameter( defaultValue = "${repositorySystemSession}", readonly = true ) + private RepositorySystemSession repoSession; + + @Parameter + private boolean useReflection; + @Override - public void execute() + public void execute() throws MojoExecutionException { + + + LOGGER.info( "session: {}", session ); + LOGGER.info( "repoSession: {}", repoSession ); LOGGER.info( "Artifact : {}", project.getArtifact() ); - LOGGER.info( "Artifacts: {}", project.getArtifacts() ); + LOGGER.info( "AttachedArtifacts: {}", project.getAttachedArtifacts() ); + LOGGER.info( "project.getFile: {}", project.getFile() ); + + Artifact pomArtifact = new DefaultArtifact( project.getArtifact().getGroupId(), + project.getArtifact().getArtifactId(), "", // classifier + "pom", project.getArtifact().getVersion() ); + pomArtifact = pomArtifact.setFile( project.getFile() ); + + + if ( useReflection ) + { + try + { + getFileTransformerFromReflection( repoSession, pomArtifact ); + } + catch ( NoSuchMethodException nme ) + { + LOGGER.info( " noSuchMethod: {}", nme.getMessage(), nme ); + } + catch ( InvocationTargetException | IllegalAccessException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + } + else + { + getFileTransformerFromMethodCall( repoSession, pomArtifact ); + } + } + + private void getFileTransformerFromMethodCall( RepositorySystemSession repoSession, Artifact pomArtifact ) + { + LOGGER.info( " -> getFileTransformerFromMethodCall" ); + FileTransformerManager fileTransformerManager = repoSession.getFileTransformerManager(); + + Collection<FileTransformer> transformersForArtifact = fileTransformerManager + .getTransformersForArtifact( pomArtifact ); + + LOGGER.info( " -> transformers: {}", transformersForArtifact ); + } + + private void getFileTransformerFromReflection( RepositorySystemSession repoSession, Artifact pomArtifact ) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + LOGGER.info( " -> getFileTransformerFromReflection" ); + + Method getFileTransformerManagerMethod = repoSession.getClass().getMethod( "getFileTransformerManager" ); + LOGGER.info( " -> getFileTransformerManagerMethod : {}", getFileTransformerManagerMethod ); + + Object fileTransformerManager = getFileTransformerManagerMethod.invoke( repoSession ); + LOGGER.info( " -> fileTransformerManager: {}", fileTransformerManager ); + if ( fileTransformerManager == null ) + { + return; + } + + Method getTransformersForArtifactMethod = fileTransformerManager.getClass() + .getMethod( "getTransformersForArtifact", Artifact.class ); + + LOGGER.info( " -> getTransformersForArtifactMethod : {}", getTransformersForArtifactMethod ); + getTransformersForArtifactMethod.setAccessible( true ); + Collection<?> transformersForArtifact = (Collection<?>) getTransformersForArtifactMethod + .invoke( fileTransformerManager, pomArtifact ); + LOGGER.info( " -> transformersForArtifact: {}", transformersForArtifact ); + + // what to do - if have more then one transformer ??? + for ( Object o : transformersForArtifact ) + { + LOGGER.info( " -> transformer: {}", o ); + Method transformDataMethod = o.getClass().getMethod( "transformData", File.class ); + transformDataMethod.setAccessible( true ); + InputStream inputStream = (InputStream) transformDataMethod.invoke( o, pomArtifact.getFile() ); + String outPom = new BufferedReader( new InputStreamReader( inputStream ) ).lines() + .collect( Collectors.joining( "\n" ) ); + LOGGER.info( "outPom:\n{}", outPom ); + } } }