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

Helidon service Shade maven plugin transformer #1064

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion etc/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@
<module name="JavadocVariable">
<property name="scope" value="protected"/>
</module>
<module name="JavadocStyle"/>
<module name="JavadocStyle">
<!-- https://github.com/checkstyle/checkstyle/issues/3351 -->
<property name="checkHtml" value="false"/>
</module>

<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
Expand Down
1 change: 1 addition & 0 deletions maven-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
<module>sitegen-maven-plugin</module>
<module>snakeyaml-codegen-maven-plugin</module>
<module>stager-maven-plugin</module>
<module>shade-extensions</module>
</modules>
</project>
40 changes: 40 additions & 0 deletions maven-plugins/shade-extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Helidon Shade Maven Plugin Extensions

Allows shading Helidon artefacts with[ Maven Shade plugin](https://maven.apache.org/plugins/maven-shade-plugin/)
by providing transformer for aggregating service registry files.

Aggregated files:
* `META-INF/helidon/service-registry.json`
* `META-INF/helidon/config-metadata.json`
* `META-INF/helidon/service.loader`

### General usage

```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="io.helidon.shade.transformers.HelidonServiceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.helidon.build-tools</groupId>
<artifactId>helidon-shade-extensions</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
```
58 changes: 58 additions & 0 deletions maven-plugins/shade-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2024 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.build-tools</groupId>
<artifactId>helidon-build-tools-project</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>helidon-shade-extensions</artifactId>
<name>Helidon Shade Maven Plugin Extensions</name>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.shade.transformers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonValue;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.resource.ReproducibleResourceTransformer;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Maven Shade plugin custom transformer for merging Helidon service-registry files.
* Usage:
* <pre><![CDATA[{@code
* <plugin>
* <groupId>org.apache.maven.plugins</groupId>
* <artifactId>maven-shade-plugin</artifactId>
* <version>3.5.1</version>
* <executions>
* <execution>
* <phase>package</phase>
* <goals>
* <goal>shade</goal>
* </goals>
* <configuration>
* <transformers>
* <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
* <transformer implementation="io.helidon.shade.transformers.HelidonServiceTransformer"/>
* </transformers>
* </configuration>
* </execution>
* </executions>
* <dependencies>
* <dependency>
* <groupId>io.helidon.build-tools</groupId>
* <artifactId>helidon-shade-extensions</artifactId>
* <version>4.0.0-SNAPSHOT</version>
* </dependency>
* </dependencies>
* </plugin>
* }]]</pre>
*/
public class HelidonServiceTransformer implements ReproducibleResourceTransformer {

static final String SERVICE_REGISTRY_PATH = "META-INF/helidon/service-registry.json";
static final String CONFIG_METADATA_PATH = "META-INF/helidon/config-metadata.json";
static final String SERVICE_LOADER_PATH = "META-INF/helidon/service.loader";

private final JsonArrayBuilder serviceRegistryArrayBuilder = Json.createArrayBuilder();
private final JsonArrayBuilder configMetadataArrayBuilder = Json.createArrayBuilder();
private final LinkedHashSet<String> serviceLoaderLines = new LinkedHashSet<>();
private boolean hasTransformedResource;

@Override
public boolean canTransformResource(String resource) {
return resource.equals(SERVICE_REGISTRY_PATH);
}

@Override
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
processResource(resource, is, relocators, 0);
}

@Override
public boolean hasTransformedResource() {
return hasTransformedResource;
}

@Override
public void modifyOutputStream(JarOutputStream jarOutputStream) throws IOException {
writeJson(SERVICE_REGISTRY_PATH, serviceRegistryArrayBuilder.build(), jarOutputStream);
writeJson(CONFIG_METADATA_PATH, configMetadataArrayBuilder.build(), jarOutputStream);
writeLines(SERVICE_LOADER_PATH, serviceLoaderLines, jarOutputStream);
}

@Override
public void processResource(String resource, InputStream is, List<Relocator> relocators, long time) throws IOException {
if (SERVICE_REGISTRY_PATH.equals(resource)) {
JsonArray array = Json.createReader(is).readArray();
for (JsonValue value : array) {
serviceRegistryArrayBuilder.add(value);
}
}

if (CONFIG_METADATA_PATH.equals(resource)) {
JsonArray array = Json.createReader(is).readArray();
for (JsonValue value : array) {
configMetadataArrayBuilder.add(value);
}
}

if (SERVICE_LOADER_PATH.equals(resource)) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF_8))) {
reader.lines().forEach(serviceLoaderLines::add);
}
}
}

private void writeLines(String path, HashSet<String> lines, JarOutputStream jos) throws IOException {
if (lines.isEmpty()) {
return;
}
hasTransformedResource = true;
JarEntry entry = new JarEntry(path);
entry.setTime(Long.MIN_VALUE);
jos.putNextEntry(entry);
try (Writer writer = new OutputStreamWriter(jos, UTF_8)) {
for (String line : lines) {
writer.append(line);
writer.append('\n');
}
}
}

private void writeJson(String path, JsonArray jsonArray, JarOutputStream jos) throws IOException {
if (jsonArray.isEmpty()) {
return;
}
hasTransformedResource = true;
JarEntry entry = new JarEntry(path);
entry.setTime(Long.MIN_VALUE);
jos.putNextEntry(entry);
Writer writer = new OutputStreamWriter(jos, UTF_8);
Json.createWriter(writer).write(jsonArray);
writer.flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Maven Shade plugin custom transformer for merging Helidon service-registry, config-metadata and service.loader files.
*/
package io.helidon.shade.transformers;
danielkec marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading