Skip to content

Commit

Permalink
[1.3.1] - 2024-04-01
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Apr 1, 2024
1 parent 5bbf5e1 commit 53f83ae
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.1] - 2024-04-01

### Added

- now plugin can read classes generated in current maven project

### Changed

- [native-helper-graalvm](https://github.com/fugerit-org/native-helper-graalvm/) version set to 1.3.1

## [0.1.0] - 2024-04-01

### Added
Expand Down
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>native-helper-maven-plugin</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>1.3.1</version>

<packaging>maven-plugin</packaging>

Expand All @@ -21,7 +21,8 @@
<properties>
<maven.compiler.release>11</maven.compiler.release>
<fj-core-version>8.5.3</fj-core-version>
<native-helper-graalvm-version>1.3.0</native-helper-graalvm-version>
<native-helper-graalvm-version>${project.version}</native-helper-graalvm-version>
<maven-core-version>3.9.6</maven-core-version>
</properties>

<licenses>
Expand Down Expand Up @@ -59,6 +60,12 @@
<artifactId>maven-plugin-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven-core-version}</version>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>native-helper-graalvm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,87 @@

import lombok.Getter;
import lombok.Setter;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
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.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.nhg.config.NativeHelperFacade;
import org.fugerit.java.nhg.config.model.NativeHelperConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

@Mojo(
name = "generate",
threadSafe = true,
defaultPhase = LifecyclePhase.COMPILE,
defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME
)
public class MojoGenerate extends AbstractMojo {

public static final String PARAM_NATIVE_HELPER_CONFIG_PATH = "nativeHelperConfigPath";


public static final String PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH = "reflectConfigJsonOutputPath";

public static final String PARAM_WARN_ON_ERROR = "warnOnError";

@Parameter(property = PARAM_NATIVE_HELPER_CONFIG_PATH, required = true )
@Getter @Setter
private String nativeHelperConfigPath;

public static final String PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH = "reflectConfigJsonOutputPath";

@Parameter(property = PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH, required = true )

@Parameter(property = PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH, required = false )
@Getter @Setter
private String reflectConfigJsonOutputPath;

@Parameter(property = PARAM_WARN_ON_ERROR, required = false )
@Getter @Setter
private boolean warnOnError;

@Parameter(defaultValue = "${project}")
@Getter @Setter
private MavenProject project;

/*
* Thanks to @viqueen for this code :
* https://medium.com/@viqueen/building-a-maven-plugin-to-explore-your-code-base-fe309ce56eb6
*/
private void setupClassLoader() throws DependencyResolutionRequiredException, IOException {
if ( this.project != null ) {
final Set<URI> urls = new LinkedHashSet<>();
// get the project's compile classpath elements
// and turn them into URLs
for (String element : project.getCompileClasspathElements()) {
urls.add(new File(element).toURI());
}
// create a URLClassLoader using the previously
// resolved urls
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
new File(project.getBuild().getOutputDirectory()).toURI().toURL()
}, new URLClassLoader( urls.stream().map( u -> SafeFunction.get( () -> u.toURL() ) ).collect( Collectors.toList() ).toArray(new URL[]{})));
// use the custom class loader
Thread.currentThread().setContextClassLoader( urlClassLoader );
}
}

public void execute() throws MojoExecutionException {
getLog().info( "using parameter "+PARAM_NATIVE_HELPER_CONFIG_PATH+" : "+this.nativeHelperConfigPath );
getLog().info( "using parameter "+PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH+" : "+this.reflectConfigJsonOutputPath );
try {
this.setupClassLoader();
File nativeHelperConfigFile = new File( this.nativeHelperConfigPath );
if ( nativeHelperConfigFile.exists() ) {
NativeHelperConfig config = NativeHelperFacade.loadConfig( this.nativeHelperConfigPath );
Expand All @@ -58,9 +101,13 @@ public void execute() throws MojoExecutionException {
} else {
throw new ConfigRuntimeException( String.format( "%s does not exist : %s", PARAM_NATIVE_HELPER_CONFIG_PATH, nativeHelperConfigFile.getCanonicalPath() ) );
}
} catch (Exception e) {
getLog().error( "Error generating configuration : "+e, e );
throw new MojoExecutionException( "Error generating code : "+e, e );
} catch (Exception | NoClassDefFoundError | ExceptionInInitializerError e) {
if ( this.isWarnOnError() ) {
getLog().error( "Error generating configuration : "+e, e );
getLog().info( "Plugin context : "+this.getPluginContext() );
} else {
throw new MojoExecutionException( "Error generating code : "+e, e );
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package test.org.fugerit.java.nativehelper.maven;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.fugerit.java.nativehelper.maven.MojoGenerate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

class TestMojoGenerate {

Expand All @@ -25,6 +30,23 @@ void test2() throws MojoExecutionException, IOException {
MojoGenerate mojo = new MojoGenerate();
File file = new File( "target/override-test/native-image/reflect-config.json" );
file.delete();
// maven project mock
mojo.setProject( new MavenProject() {
@Override
public Build getBuild() {
return new Build() {
@Override
public String getOutputDirectory() {
return "target";
}
};
}

@Override
public List<String> getCompileClasspathElements() throws DependencyResolutionRequiredException {
return Arrays.asList( new File( this.getBuild().getOutputDirectory(),"classes" ).getAbsolutePath() );
}
} );
mojo.setNativeHelperConfigPath( "src/test/resources/tool/config/native-helper-config.yaml" );
mojo.setReflectConfigJsonOutputPath( file.getCanonicalPath() );
mojo.execute();
Expand All @@ -45,4 +67,13 @@ void testFail2() throws MojoExecutionException, IOException {
Assertions.assertThrows( MojoExecutionException.class, () -> mojo.execute() );
}

@Test
void testFail3() throws MojoExecutionException, IOException {
MojoGenerate mojo = new MojoGenerate();
mojo.setNativeHelperConfigPath( "src/test/resources/tool/config/native-helper-config-fail.yaml" );
mojo.setWarnOnError( true );
mojo.execute();
Assertions.assertNotNull( mojo );
}

}

0 comments on commit 53f83ae

Please sign in to comment.