Skip to content

Commit 876d667

Browse files
committed
Add additional scope for Maven plugin.
1 parent 665a090 commit 876d667

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

byte-buddy-maven-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The **Byte Buddy Maven Plugin** enables you to apply bytecode enhancements durin
3232
</build>
3333
```
3434

35-
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.
35+
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.
3636

3737
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.
3838

byte-buddy-maven-plugin/src/main/java/net/bytebuddy/build/maven/ByteBuddyMojo.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.bytebuddy.utility.CompoundList;
2828
import net.bytebuddy.utility.nullability.MaybeNull;
2929
import net.bytebuddy.utility.nullability.UnknownNull;
30+
import org.apache.maven.artifact.Artifact;
3031
import org.apache.maven.artifact.DependencyResolutionRequiredException;
3132
import org.apache.maven.model.Dependency;
3233
import org.apache.maven.model.PluginManagement;
@@ -35,7 +36,11 @@
3536
import org.apache.maven.plugin.MojoExecutionException;
3637
import org.apache.maven.plugin.MojoFailureException;
3738
import org.apache.maven.plugin.logging.Log;
38-
import org.apache.maven.plugins.annotations.*;
39+
import org.apache.maven.plugins.annotations.Component;
40+
import org.apache.maven.plugins.annotations.LifecyclePhase;
41+
import org.apache.maven.plugins.annotations.Mojo;
42+
import org.apache.maven.plugins.annotations.Parameter;
43+
import org.apache.maven.plugins.annotations.ResolutionScope;
3944
import org.apache.maven.project.MavenProject;
4045
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
4146
import org.codehaus.plexus.util.Scanner;
@@ -44,11 +49,23 @@
4449
import org.eclipse.aether.RepositorySystemSession;
4550
import org.sonatype.plexus.build.incremental.BuildContext;
4651

47-
import java.io.*;
52+
import java.io.BufferedReader;
53+
import java.io.File;
54+
import java.io.FileInputStream;
55+
import java.io.IOException;
56+
import java.io.InputStream;
57+
import java.io.InputStreamReader;
4858
import java.net.MalformedURLException;
4959
import java.net.URL;
5060
import java.net.URLClassLoader;
51-
import java.util.*;
61+
import java.util.ArrayList;
62+
import java.util.Collections;
63+
import java.util.Enumeration;
64+
import java.util.HashMap;
65+
import java.util.HashSet;
66+
import java.util.List;
67+
import java.util.Map;
68+
import java.util.Set;
5269
import java.util.jar.JarEntry;
5370
import java.util.jar.JarFile;
5471

@@ -596,7 +613,6 @@ protected List<String> getClassPathElements() throws MojoFailureException {
596613
throw new MojoFailureException("Could not resolve class path", e);
597614
}
598615
}
599-
600616
}
601617

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

609625
@Override
610-
protected List<String> getClassPathElements() throws MojoFailureException {
626+
protected List<String> getClassPathElements() {
611627
try {
612628
return project.getRuntimeClasspathElements();
613629
} catch (DependencyResolutionRequiredException e) {
614-
throw new MojoFailureException("Could not resolve class path", e);
630+
throw new RuntimeException(e);
631+
}
632+
}
633+
}
634+
635+
/**
636+
* A Byte Buddy plugin that transforms a project's production class files where all scopes but the test scope are included.
637+
*/
638+
@Mojo(name = "transform-extended", defaultPhase = LifecyclePhase.PROCESS_CLASSES, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
639+
public static class WithExtendedDependencies extends ForProductionTypes {
640+
641+
@Override
642+
protected List<String> getClassPathElements() {
643+
List<String> classPath = new ArrayList<String>(project.getArtifacts().size() + 1);
644+
String directory = project.getBuild().getOutputDirectory();
645+
if (directory != null) {
646+
classPath.add(directory);
647+
}
648+
for (Artifact artifact : project.getArtifacts()) {
649+
if (artifact.getArtifactHandler().isAddedToClasspath()
650+
&& !Artifact.SCOPE_TEST.equals(artifact.getScope())
651+
&& !Artifact.SCOPE_IMPORT.equals(artifact.getScope())) {
652+
File file = artifact.getFile();
653+
if (file != null) {
654+
classPath.add(file.getPath());
655+
}
656+
}
615657
}
658+
return classPath;
616659
}
617660
}
618661
}

0 commit comments

Comments
 (0)