Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC - JAR of JARs #123

Draft
wants to merge 2 commits into
base: 4.6.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions entrypoint-test/micronaut-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
applicationType: cli
defaultPackage: foo
testFramework: junit
sourceLanguage: java
buildTool: maven
features: [app-name, java, junit, logback, maven, picocli, picocli-java-application, picocli-junit, readme, shade, yaml]
178 changes: 178 additions & 0 deletions entrypoint-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-entrypoint-test</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<name>Micronaut Entrypoint Test</name>

<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>2.3.3</version>
</parent>

<properties>
<packaging>jar</packaging>
<jdk.version>1.8</jdk.version>
<!-- If you are building with JDK 9 or higher, you can uncomment the lines below to set the release version -->
<!-- <release.version>8</release.version> -->
<micronaut.version>2.3.3</micronaut.version>
<picocli.version>4.6.1</picocli.version>
<exec.mainClass>io.micronaut.build.entrypoint.test.MainCommand</exec.mainClass>
<micronaut-maven-plugin.version>1.1.7-SNAPSHOT</micronaut-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.picocli</groupId>
<artifactId>micronaut-picocli</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.children="append">
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>${picocli.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=foo</arg>
<arg>-Amicronaut.processing.module=foo</arg>
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!-- Copy dependencies to classes/libs so that they end up being inside the JAR -->
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/libs</outputDirectory>
<excludeArtifactIds>micronaut-entrypoint</excludeArtifactIds>
</configuration>
</execution>

<!-- Unpack micronaut-entrypoint in classes directory, since it needs to be exploded to be the JAR's main class -->
<execution>
<id>unpack-entrypoint</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<artifactItems>
<artifactItem>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-entrypoint</artifactId>
<version>${micronaut-maven-plugin.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

<!-- Configure the JAR properly -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>io.micronaut.build.entrypoint.Entrypoint</mainClass>
</manifest>
<manifestEntries>
<Micronaut-Entrypoint>${exec.mainClass}</Micronaut-Entrypoint>
</manifestEntries>
</archive>
</configuration>
</plugin>

<!-- Disable Shade plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>default-shade</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.micronaut.build.entrypoint.test;

import io.micronaut.configuration.picocli.PicocliRunner;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "io/micronaut/build/entrypoint/test", description = "...",
mixinStandardHelpOptions = true)
public class MainCommand implements Runnable {

@Option(names = {"-v", "--verbose"}, description = "...")
boolean verbose;

public static void main(String[] args) throws Exception {
PicocliRunner.run(MainCommand.class, args);
}

public void run() {
// business logic here
if (verbose) {
System.out.println("Hi!");
}
}
}
3 changes: 3 additions & 0 deletions entrypoint-test/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
micronaut:
application:
name: foo
20 changes: 20 additions & 0 deletions entrypoint/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin-parent</artifactId>
<version>1.1.7-SNAPSHOT</version>
</parent>

<artifactId>micronaut-entrypoint</artifactId>
<packaging>jar</packaging>

<name>Micronaut Entrypoint</name>
<description>Micronaut Entrypoint is the piece that runs Micronaut applications packaged as a JAR of JARs
</description>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.micronaut.build.entrypoint;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;

public class Entrypoint {

private static final String MICRONAUT_ENTRYPOINT = "Micronaut-Entrypoint";
private static final String CLASS_PATH = "Class-Path";

public static void main(String[] args) throws Exception {
Entrypoint entrypoint = new Entrypoint();
entrypoint.run(args);
}

public void run(String[] args) throws Exception {
URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
JarFile file = new JarFile(Paths.get(jarUrl.toURI()).toFile());
Attributes manifest = file.getManifest().getMainAttributes();
URLClassLoader classLoader = createClassLoader(manifest, jarUrl);
invokeEntrypoint(args, manifest, classLoader);
}

private void invokeEntrypoint(String[] args, Attributes manifest, URLClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
String entrypoint = manifest.getValue(MICRONAUT_ENTRYPOINT);
Class<?> entrypointClass = Class.forName(entrypoint, false, classLoader);
Method main = entrypointClass.getDeclaredMethod("main", String[].class);
main.setAccessible(true);
main.invoke(null, new Object[] {args});
}

private URLClassLoader createClassLoader(Attributes manifest, URL jarUrl) throws IOException {
List<URL> urls = new ArrayList<>();
urls.add(jarUrl);
copyDependencies(manifest, urls);
URLClassLoader classLoader = URLClassLoader.newInstance(urls.toArray(new URL[0]), ClassLoader.getSystemClassLoader().getParent());
Thread.currentThread().setContextClassLoader(classLoader);
return classLoader;
}

private void copyDependencies(Attributes manifest, List<URL> urls) throws IOException {
for (String dependency : manifest.getValue(CLASS_PATH).split(" ")) {
URL url = getClass().getClassLoader().getResource(dependency);
if (url != null) {
Path path = Files.createTempFile(dependency.substring(dependency.indexOf("/") + 1, dependency.lastIndexOf(".")), ".jar");
path.toFile().deleteOnExit();
try (InputStream is = url.openStream()) {
Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING);
}
urls.add(path.toUri().toURL());
}
}
}

}
Loading