Skip to content

Commit

Permalink
Add additional scope for Maven plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Mar 6, 2023
1 parent 665a090 commit 876d667
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion byte-buddy-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The **Byte Buddy Maven Plugin** enables you to apply bytecode enhancements durin
</build>
```

This example transformation uses a standard plugin that is shipped with Byte Buddy, which caches any method return value if annotated by `CachedReturnPlugin.Enhance`. The plugin in the example is applied onto the main source set, test classes can be transformed by specifying the *transform-test* goal. Custom plugins must implement Byte Buddy's `Plugin` interface where the plugin's location is specified by Maven artifact coordinates as shown in the example. The coordinates can be dropped if the plugin is contained within the transformed artifact itself.
This example transformation uses a standard plugin that is shipped with Byte Buddy, which caches any method return value if annotated by `CachedReturnPlugin.Enhance`. The plugin in the example is applied onto the main source set, test classes can be transformed by specifying the *transform-test* goal. Furthermore, it is possible to transform production classes while retaining runtime classes by using *transform-runtime*, or to retain dependencies of all non-test scopes by *transform-extended*. Custom plugins must implement Byte Buddy's `Plugin` interface where the plugin's location is specified by Maven artifact coordinates as shown in the example. The coordinates can be dropped if the plugin is contained within the transformed artifact itself.

A plugin can declare a constructor that takes arguments of type `File`, `BuildLogger` or a Gradle-specific `Logger` where the class file root directory or an appropriate logger is provided. It is also possible to supply an argument explicitly by specifying an argument in the plugin configuration.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.bytebuddy.utility.CompoundList;
import net.bytebuddy.utility.nullability.MaybeNull;
import net.bytebuddy.utility.nullability.UnknownNull;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.PluginManagement;
Expand All @@ -35,7 +36,11 @@
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.*;
import org.apache.maven.plugins.annotations.Component;
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.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.util.Scanner;
Expand All @@ -44,11 +49,23 @@
import org.eclipse.aether.RepositorySystemSession;
import org.sonatype.plexus.build.incremental.BuildContext;

import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

Expand Down Expand Up @@ -596,7 +613,6 @@ protected List<String> getClassPathElements() throws MojoFailureException {
throw new MojoFailureException("Could not resolve class path", e);
}
}

}

/**
Expand All @@ -607,12 +623,39 @@ protected List<String> getClassPathElements() throws MojoFailureException {
public static class WithRuntimeDependencies extends ForProductionTypes {

@Override
protected List<String> getClassPathElements() throws MojoFailureException {
protected List<String> getClassPathElements() {
try {
return project.getRuntimeClasspathElements();
} catch (DependencyResolutionRequiredException e) {
throw new MojoFailureException("Could not resolve class path", e);
throw new RuntimeException(e);
}
}
}

/**
* A Byte Buddy plugin that transforms a project's production class files where all scopes but the test scope are included.
*/
@Mojo(name = "transform-extended", defaultPhase = LifecyclePhase.PROCESS_CLASSES, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public static class WithExtendedDependencies extends ForProductionTypes {

@Override
protected List<String> getClassPathElements() {
List<String> classPath = new ArrayList<String>(project.getArtifacts().size() + 1);
String directory = project.getBuild().getOutputDirectory();
if (directory != null) {
classPath.add(directory);
}
for (Artifact artifact : project.getArtifacts()) {
if (artifact.getArtifactHandler().isAddedToClasspath()
&& !Artifact.SCOPE_TEST.equals(artifact.getScope())
&& !Artifact.SCOPE_IMPORT.equals(artifact.getScope())) {
File file = artifact.getFile();
if (file != null) {
classPath.add(file.getPath());
}
}
}
return classPath;
}
}
}
Expand Down

0 comments on commit 876d667

Please sign in to comment.