Skip to content

Commit

Permalink
Helidon service Shade maven plugin transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Aug 26, 2024
1 parent 5791371 commit 7f2c144
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
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>
35 changes: 35 additions & 0 deletions maven-plugins/shade-extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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 combining `META-INF/helidon/service-registry.json` files to single one.

### 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>
```
42 changes: 42 additions & 0 deletions maven-plugins/shade-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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</groupId>
<artifactId>yasson</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
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;

/**
* Maven Shade plugin custom transformer for merging Helidon service-registry files.
* Usage:
* <pre>{@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 {

private static final String PATH = "META-INF/helidon/service-registry.json";
private final JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
private JsonArray result = null;

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

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

@Override
public boolean hasTransformedResource() {
return getResult().isEmpty();
}

@Override
public void modifyOutputStream(JarOutputStream jarOutputStream) throws IOException {
JarEntry jarEntry = new JarEntry(PATH);
jarEntry.setTime(Long.MIN_VALUE);
jarOutputStream.putNextEntry(jarEntry);
Writer writer = new OutputStreamWriter(jarOutputStream, StandardCharsets.UTF_8);
Json.createWriter(writer).write(getResult());
writer.close();
}

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

private JsonArray getResult() {
if (result == null) {
result = jsonArrayBuilder.build();
}

return result;
}

}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@
<version.lib.spotbugs-annotations>3.1.12</version.lib.spotbugs-annotations>
<version.lib.wagon-http>3.5.3</version.lib.wagon-http>
<version.lib.commons-text>1.10.0</version.lib.commons-text>
<version.lib.maven-shade-plugin>3.5.1</version.lib.maven-shade-plugin>
<version.lib.yasson>3.0.4</version.lib.yasson>

<!--
!Version statement! - end
Expand Down Expand Up @@ -942,6 +944,16 @@
<artifactId>commons-text</artifactId>
<version>${version.lib.commons-text}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${version.lib.maven-shade-plugin}</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>${version.lib.yasson}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 7f2c144

Please sign in to comment.