Access project classes from a maven plugin - java

I'm making a maven plugin that run in test phase, in pom.xml configuration of a project that uses my plugin, I'm setting a class canonical name that I want to use to run that class from my plugin, basically I'm making a way to have a dynamic class loading of classes inside the project from my plugin.
Class clazz = Class.forName("... class from pom.xml ...")
When I run it I receive the expectable "ClassNotFoundException", seems the class loader are not the same or not shared.
There is a way to do it? Like capture the class loader from the project or receive it by dependency injection into my plugin? What is the best way?

We can use the Hibernate implementation in mojo can be used as a reference to make it:
Checkout the source code here: http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/hibernate3-maven-plugin/2.2/org/codehaus/mojo/hibernate3/HibernateExporterMojo.java#HibernateExporterMojo.getClassLoader%28%29
private ClassLoader getClassLoader(MavenProject project)
{
try
{
List classpathElements = project.getCompileClasspathElements();
classpathElements.add( project.getBuild().getOutputDirectory() );
classpathElements.add( project.getBuild().getTestOutputDirectory() );
URL urls[] = new URL[classpathElements.size()];
for ( int i = 0; i < classpathElements.size(); ++i )
{
urls[i] = new File( (String) classpathElements.get( i ) ).toURL();
}
return new URLClassLoader( urls, this.getClass().getClassLoader() );
}
catch ( Exception e )
{
getLog().debug( "Couldn't get the classloader." );
return this.getClass().getClassLoader();
}
}
To capture the "project" object, we can use the mojo dependency injection:
/**
* Dependency injected
*/
#Parameter(defaultValue = "${project}")
public MavenProject project;
And use it to load some class in project class loader:
getClassLoader(this.project).loadClass("com.somepackage.SomeClass")

Related

Configure Gradle Plugin inside Gradle plugin

I want to create a custom Gradle plugin that will encapsulate Checkstyle and PMD configurations. So, other projects can just apply one custom plugin without bothering about any additional configurations.
I applied checkstyle plugin.
plugins {
id 'java-gradle-plugin'
id 'checkstyle'
}
And then I applied it inside my custom plugin.
public class CustomPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPluginManager().apply(CheckstylePlugin.class);
}
}
When I try to build the project I get an error.
Unable to find: config/checkstyle/checkstyle.xml
How can I override other plugin's properties? For example, I want to change the default checkstyle.xml path. I can do it manually inside build.gradle of the plugin project itself. But in this case, other projects that apply the plugin won't have this configurations defined by default (I tested it).
EDIT 1:
I managed to configure checkstyle plugin with ChecktyleExtension.
public class MetricCodingRulesGradlePluginPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPluginManager().apply("checkstyle");
project.getExtensions().configure(CheckstyleExtension.class, checkstyleExtension -> {
checkstyleExtension.setConfigFile(new File("style/checkstyle.xml"));
});
}
}
checkstyle.xml is placed in the plugin project. When I try to apply it within any other project, checkstyle searches it inside the current project directory but not the plugin's one. Is it possible to overcome this issue? I don't want users of that plugin to put any additional files inside their project.
EDIT 2:
I put the config files to resources folder and tried to read the content.
public class MetricCodingRulesGradlePluginPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPluginManager().apply("checkstyle");
project.getExtensions().configure(CheckstyleExtension.class, checkstyleExtension -> {
URL url = getClass().getClassLoader().getResource("style/checkstyle.xml");
System.out.println("URL: " + url);
try {
checkstyleExtension.setConfigFile(
Paths.get(url.toURI())
.toFile()
);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
});
}
}
When I apply the plugin to another project, I get the following error:
URL: jar:file:/Users/user/.gradle/caches/jars-9/8f4176a8ae146bf601f1214b287eb805/my-plugin-0.0.1-SNAPSHOT.jar!/style/checkstyle.xml
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
Java cannot read the file from the jar archive for some reason. Any approaches to overcome this error?
You'd need to bundle the checkstyle.xml within your plugin's resources folder, so when you ship it, you can always access it from within the plugin code.
Basically, you need to put the config under src/main/resources/checkstyle.xml of the plugin and then access it like this:
URL resourceURL = getClass().getClassLoader().getResource("checkstyle.xml");
if (resourceURL != null) {
File resourceFile = File(resourceURL.getFile());
checkstyleExtension.setConfigFile(resourceFile);
}
Also remember, if you ship your plugin as a .jar, you'd need to unpack the checkstyle.xml into a temp file beforehand. Roughly:
File temp = File.createTempFile(".checkstyle", ".xml")
try (FileOutputStream out = new FileOutputStream(temp)) {
try (InputStream resourceStream = getClass().getClassLoader().getResourceAsStream("checkstyle.xml")) {
byte[] buffer = new byte[1024];
int bytes = resourceStream.read(buffer);
while (bytes >= 0) {
out.write(buffer, 0, bytes);
bytes = resourceStream.read(buffer);
}
}
}

classLoader can't load classes implementing a class out of this module

I'm trying to load specific classes in my maven plugin using below class loader:
public ClassLoader getClassLoader(MavenProject project) {
try
{
List classpathElements = project.getCompileClasspathElements();
URL urls[] = new URL[classpathElements.size()];
for ( int i = 0; i < classpathElements.size(); ++i ) {
urls[i] = new File( (String) classpathElements.get( i ) ).toURL();
}
return new URLClassLoader( urls, this.getClass().getClassLoader() );
} catch ( Exception e ) {
System.out.println( "Couldn't get the classloader." );
return this.getClass().getClassLoader();
}
}
this loader works completely fine in a test simple project. but when I use it in a multi-module project it doesn't load specific classes. the classes that implement a class in another module(for example CardlessFacadeBean implements CardlessFacade, CardlessFacadeBean class is in this module and CardlessFacade class is in another module). but other classes that don't have this condition loads fine. is there any way to solve this issue in a simple way? Thanks so much
I simply added another module classpath to classpath Elements list as below and it recognized mentioned classes.
classpathElements.add(moduleDirectory);
any other solution would be appreciated.

Gradle Java Custom Plugin: find URL of a dependency and recursiverly all the ones it depends on

I am creating a Gradle plugin (Y) in Java that is equivalent to a Maven one I previously implemented. Given a dependency (X) of the plugin (Y), I need to find its URL and the URL of all the dependencies X depends on. This is needed to create a full classpath for X (which is a Java application) to execute it on a new JVM/process.
In a Maven plugin, this is achieved with:
//injected objects
//#Parameter(defaultValue = "${plugin.artifacts}", required = true, readonly = true)
//private List<Artifact> artifacts;
//#Component
//private ProjectBuilder projectBuilder;
//#Parameter(defaultValue="${repositorySystemSession}", required = true, readonly = true)
//private RepositorySystemSession repoSession;
// first find the artifact, ie the jar of the dependency
Artifact x = null;
for(Artifact art : artifacts){
if(art.getArtifactId().equals("name-of-the-X-artifact")){
x = art;
break;
}
}
/*
* build a project descriptor for the artifact, which is needed to
* query all of its dependencies
*/
DefaultProjectBuildingRequest req = new DefaultProjectBuildingRequest();
req.setRepositorySession(repoSession);
req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
req.setSystemProperties(System.getProperties());
req.setResolveDependencies(true);
ProjectBuildingResult res = projectBuilder.build(x, req);
// finally, determine the full classpath
String cp = x.getFile().getAbsolutePath();
for(Artifact dep : res.getProject().getArtifacts()){
cp += File.pathSeparator + dep.getFile().getAbsolutePath();
}
How to achieve the same in Gradle? Given:
public class MyPluginY implements Plugin<Project> {
#Override
public void apply(Project p) {
}
}
I can iterate through its dependencies with for(Configuration c : p.getBuildscript().getConfigurations()) and then for(Dependency d: c.getAllDependencies()). However:
How to determine the URL of d?
How to recursively find all the dependencies of d?

Working with plugin ClassLoader and project ClassLoader

So I am developing one maven plugin where I need to modify the classloaders in order to work correctly. The problem is that I am not sure that I am modifying the correct classloader. What I'm doing is the following:
#Mojo(name = "aggregate", requiresDependencyResolution = ResolutionScope.TEST)
public class AcceptanceTestMojo extends AbstractMojo {
private static final String SYSTEM_CLASSLOADER_FIELD_NAME = "scl";
#Parameter
private String property;
#Component
public PluginDescriptor pluginDescriptor;
#Component
public MavenProject mavenProject;
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
ClassLoader newClassLoader = null;
List<String> runtimeClassPathElements;
try {
runtimeClassPathElements = mavenProject.getTestClasspathElements();
} catch (DependencyResolutionRequiredException e) {
throw new MojoFailureException(MojoFailureMessages.UNRESOLVED_DEPENDENCIES_MESSAGE);
}
ClassRealm realm = pluginDescriptor.getClassRealm();
ClassRealm modifiedRealm= new ClassRealm( realm.getWorld(), realm.getId(), realm.getParentClassLoader());
try {
for (String element : runtimeClassPathElements) {
File elementFile = new File(element);
modifiedRealm.addURL(elementFile.toURI().toURL());
}
} catch (MalformedURLException e) {
throw new MojoFailureException(MojoFailureMessages.UNRESOLVED_CLASSES_MESSAGE);
}
pluginDescriptor.setClassRealm(modifiedRealm);
So I am getting the ClassRealm and I'am making slight changes to the UCP(removing some jars) and after that I set the newly created ClassRealm to the project descriptor. I am also changing the ContextClassLoader and the SystemClassLoader as the project I am executing my plugin on are using them for some interactions. These two are working fine- they are changed and the plugin is working fine with them. The problem is the plugin classloader. Because for some reason when executing my plugin on one project it is looking in the plugin ClassRealm and searching for the needed jars from there. But the code I put above is not fully correct, because when I come to the part where the execution of the plugin is looking in the plugin ClassRealm it is not the modified one- it gets another reference, which I don't know where it comes from. What I think is that I am not setting the ClassRealm correctly or I am missing something else.

Loading classes dynamically from jar

I know that we can load classes dynamically by using custom class loaders.
But here my problem is my Class itself is depends upon other classes
My task is to get PigServer object .So I have used following code to load PigServer class
_pigServerClass = _classLoader.loadClass("org.apache.pig.PigServer");
But here PigServer class itself is depends upon so many other classes.
So when i am trying to get instance of PigServer class then it is showing following errors
java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
java.lang.ClassNotFoundException:org.apache.log4j.AppenderSkeleton
etc..
Can anyone tell how to solve this?
There seems to be a misunderstanding. If you have all the jars required in a folder, say "lib", you can for example set up a class loader like this:
File libs = new File("lib");
File[] jars = libs.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().toLowerCase().endsWith(".jar");
}
});
URL[] urls = new URL[jars.length];
for (int i=0; i<jars.length; i++) {
urls[i] = jars[i].toURI().toURL();
}
ClassLoader uc = new URLClassLoader(urls,this.getClass().getClassLoader());
Class<?> pigServerClz = Class.forName("org.apache.pig.PigServer", false, uc);
Object pigServer = pigServerClz.newInstance();
// etc...
How you created your ClassLoader?
Did you specified another "parent" classloader, on wich classloading can be delegated?

Categories

Resources