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 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
7 changes: 5 additions & 2 deletions etc/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2016, 2022 Oracle and/or its affiliates.
Copyright (c) 2016, 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.
Expand Down 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 etc/copyright-exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ expected
expected-config
.helidon
jvm.config
.loader
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>
42 changes: 42 additions & 0 deletions maven-plugins/shade-extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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`
* `META-INF/helidon/serial-config.properties`
* `META-INF/helidon/feature-metadata.properties`

### 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.build.maven.shade.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>
```
48 changes: 48 additions & 0 deletions maven-plugins/shade-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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.glassfish</groupId>
<artifactId>javax.json</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.build.maven.shade;

import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarOutputStream;

interface Aggregator {
String path();
boolean hasTransformedResource();
void aggregate(InputStream is) throws IOException;
void writeToJar(JarOutputStream jos) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.build.maven.shade;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

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

/**
* Feature properties can't be effectively aggregated, instead single feature file is created.
*
* Example:
* <pre>{@code
* # Features aggregated by HelidonServiceTransformer during shading:
* # - CDI
* # - Config
* # - Object Mapping
* # - YAML
* # - Observe
* # - CORS
* # - WebServer
* # - Media
* # - Encoding
* # - Static Content
* # - JSONP
* # - Config
* # - Encryption
* # - WebClient
* # - HTTP/1.1
* n=Helidon Shaded
* d=Helidon modules shaded in to the single module.
* }</pre>
*/
class FeatureMetadataAggregator implements Aggregator {

static final String FEATURE_METADATA_PATH = "META-INF/helidon/feature-metadata.properties";
private final List<String> features = new LinkedList<>();

@Override
public String path() {
return FEATURE_METADATA_PATH;
}

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

@Override
public void aggregate(InputStream is) throws IOException {
Properties properties = new Properties();
properties.load(is);
Optional.ofNullable(properties.getProperty("n")).ifPresent(features::add);
}

@Override
public void writeToJar(JarOutputStream jos) throws IOException {
JarEntry entry = new JarEntry(path());
entry.setTime(Long.MIN_VALUE);
jos.putNextEntry(entry);
Writer writer = new OutputStreamWriter(jos, UTF_8);
writer.write("# Features aggregated by HelidonServiceTransformer during shading:");
writer.write("\n# - " + String.join("\n# - ", features));
writer.write("\nn=Helidon Shaded");
writer.write("\nd=Helidon modules shaded in to the single module.");
writer.write("\n");
writer.flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.build.maven.shade;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.jar.JarOutputStream;
import java.util.stream.Stream;

import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.resource.ReproducibleResourceTransformer;

import static java.util.stream.Collectors.toMap;

/**
* 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.build.maven.shade.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";

private final Map<String, Aggregator> aggregators = Stream
.of(
new JsonArrayAggregator(SERVICE_REGISTRY_PATH),
new JsonArrayAggregator(CONFIG_METADATA_PATH),
new ServiceLoaderAggregator(),
new SerialConfigAggregator(),
new FeatureMetadataAggregator()
)
.collect(toMap(Aggregator::path, Function.identity()));

@Override
public boolean canTransformResource(String resource) {
return aggregators.containsKey(resource);
}

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

@Override
public boolean hasTransformedResource() {
return aggregators.values().stream().anyMatch(Aggregator::hasTransformedResource);
}

@Override
public void modifyOutputStream(JarOutputStream jarOutputStream) throws IOException {
for (Aggregator a : aggregators.values()) {
a.writeToJar(jarOutputStream);
}
}

@Override
public void processResource(String resource, InputStream is, List<Relocator> relocators, long time) throws IOException {
aggregators.get(resource).aggregate(is);
}
}
Loading