Skip to content
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
2 changes: 1 addition & 1 deletion .sling-module.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"jenkins": {
"jdks": [8, 11, 13]
"jdks": [11, 17, 21]
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ Sling application. When executed, the plugin will define two project properties
`org.apache.sling.scriptingbundle.maven.plugin.Provide-Capability`) which can be used to
populate the corresponding bundle headers.

An optional `serviceRanking` can be configured to apply a service ranking to all bundled
scripts in the bundle via the `Sling-Bundled-Scripts-Ranking` bundle header.

For more details head over to the documentation page from https://sling.apache.org/components/scriptingbundle-maven-plugin/.
21 changes: 19 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<properties>
<sling.java.version>8</sling.java.version>
<maven.version>3.6.0</maven.version>
<maven.version>3.9.16</maven.version>
<site.jira.version.id>12344664,12347157,12348422,12350010,12350474,12350606</site.jira.version.id>
<maven.site.path>${project.artifactId}-archives/${project.artifactId}-LATEST</maven.site.path>
<project.build.outputTimestamp>2021-12-16T14:04:46Z</project.build.outputTimestamp>
Expand Down Expand Up @@ -108,7 +108,7 @@
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.3.0</version>
<version>4.10.3.0</version>
<configuration>
<effort>Max</effort>
<xmlOutput>true</xmlOutput>
Expand Down Expand Up @@ -167,30 +167,47 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.20.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.22.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
private Map<String, String> scriptEngineMappings;

@Override
public boolean analyzeJar(Analyzer analyzer) throws Exception {

Check failure on line 62 in src/main/java/org/apache/sling/scriptingbundle/plugin/bnd/BundledScriptsScannerPlugin.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_sling-scriptingbundle-maven-plugin&issues=AZ_cqf8TO7ixMsVbteRY&open=AZ_cqf8TO7ixMsVbteRY&pullRequest=15
logger = new BndLogger(reporter);
boolean inContentPackage = "content-package".equals(analyzer.get("project.packaging"));
Path workDirectory = Paths.get(analyzer.get(PROJECT_BUILD_FOLDER), "scriptingbundle-maven-plugin");
Expand All @@ -79,7 +79,10 @@
try {
if (!Files.isDirectory(file)) {
Path workingCopy = Paths.get(workDirectory.toString(), sourceDirectoryPath.relativize(file).toString());
Files.createDirectories(workingCopy.getParent());
Path parentDir = workingCopy.getParent();
if (parentDir != null) {
Files.createDirectories(parentDir);
}
Files.copy(file, workingCopy, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
Expand Down Expand Up @@ -113,6 +116,11 @@
requiredCapabilities = requiredCapabilitiesDefinition;
}
analyzer.set(aQute.bnd.osgi.Constants.REQUIRE_CAPABILITY, requiredCapabilities);

Integer serviceRanking = getConfiguredServiceRanking();
if (serviceRanking != null) {
analyzer.set(Constants.SLING_BUNDLED_SCRIPTS_RANKING_HEADER, serviceRanking.toString());
}
return false;
}

Expand Down Expand Up @@ -198,6 +206,18 @@
return true;
}

private Integer getConfiguredServiceRanking() {
String serviceRankingString = pluginProperties.get(Constants.BND_SERVICE_RANKING);
if (StringUtils.isNotEmpty(serviceRankingString)) {
try {
return Integer.valueOf(serviceRankingString.trim());
} catch (NumberFormatException e) {
logger.error(String.format("Invalid service ranking: %s.", serviceRankingString));
}
}
return null;
}

private Stream<Path> walkPath(Path path, Set<PathMatcher> includes, Set<PathMatcher> excludes) throws IOException {
return Files.walk(path).filter(file -> {
boolean include = includes.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,20 @@
@Parameter(property = "scriptingbundle.missingRequirementsOptional", defaultValue = "true")
private boolean missingRequirementsOptional = true;

/**
* When set, the given numeric value is exposed via the {@code org.apache.sling.scriptingbundle.maven.plugin.Sling-Bundled-Scripts-Ranking}
* Maven project property, which can be used to generate the {@code Sling-Bundled-Scripts-Ranking} OSGi bundle header. That header
* applies globally to all scripts in the bundle and allows influencing the service ranking of the servlets registered for the
* bundled scripts. When not set, the property is not generated.
*
* @since 0.6.0
*/
@Parameter(property = "scriptingbundle.serviceRanking")
private Integer serviceRanking;

private Capabilities capabilities;

public void execute() {

Check failure on line 191 in src/main/java/org/apache/sling/scriptingbundle/plugin/maven/MetadataMojo.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_sling-scriptingbundle-maven-plugin&issues=AZ_cqf5AO7ixMsVbteRX&open=AZ_cqf5AO7ixMsVbteRX&pullRequest=15
boolean inContentPackage = "content-package".equals(project.getPackaging());
Logger logger = new MavenLogger(getLog());
Path workDirectory = Paths.get(project.getBuild().getDirectory(), "scriptingbundle-maven-plugin");
Expand All @@ -199,7 +210,10 @@
try {
if (!Files.isDirectory(file)) {
Path workingCopy = workDirectory.resolve(sourceDirectoryPath.relativize(file));
Files.createDirectories(workingCopy.getParent());
Path parentDir = workingCopy.getParent();
if (parentDir != null) {
Files.createDirectories(parentDir);
}
Files.copy(file, workingCopy, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
Expand Down Expand Up @@ -232,10 +246,14 @@
);
String providedCapabilitiesDefinition = capabilities.getProvidedCapabilitiesString();
String requiredCapabilitiesDefinition = capabilities.getRequiredCapabilitiesString();
project.getProperties().put("org.apache.sling.scriptingbundle.maven.plugin." + org.osgi.framework.Constants.PROVIDE_CAPABILITY,

Check failure on line 249 in src/main/java/org/apache/sling/scriptingbundle/plugin/maven/MetadataMojo.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "org.apache.sling.scriptingbundle.maven.plugin." 3 times.

See more on https://sonarcloud.io/project/issues?id=apache_sling-scriptingbundle-maven-plugin&issues=AZ_cqf5AO7ixMsVbteRW&open=AZ_cqf5AO7ixMsVbteRW&pullRequest=15
providedCapabilitiesDefinition);
project.getProperties().put("org.apache.sling.scriptingbundle.maven.plugin." + org.osgi.framework.Constants.REQUIRE_CAPABILITY,
requiredCapabilitiesDefinition);
if (serviceRanking != null) {
project.getProperties().put("org.apache.sling.scriptingbundle.maven.plugin." + Constants.SLING_BUNDLED_SCRIPTS_RANKING_HEADER,
serviceRanking.toString());
}
} catch (IOException e) {
logger.error("Unable to generate working directory.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ private Constants() {}
public static final String BND_SCRIPT_ENGINE_MAPPINGS = "scriptEngineMappings";
public static final String BND_SEARCH_PATHS = "searchPaths";
public static final String BND_MISSING_REQUIREMENTS_OPTIONAL = "missingRequirementsOptional";
public static final String BND_SERVICE_RANKING = "serviceRanking";

public static final String SLING_BUNDLED_SCRIPTS_RANKING_HEADER = "Sling-Bundled-Scripts-Ranking";
public static final String SLING_RESOURCE_SUPER_TYPE_XML_LOCAL_NAME = "resourceSuperType";
public static final String SLING_REQUIRED_RESOURCE_TYPES = "sling:requiredResourceTypes";
public static final String SLING_REQUIRED_RESOURCE_TYPES_XML_LOCAL_NAME = "requiredResourceTypes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class VaultContentXmlReader {
public final class VaultContentXmlReader {

private static final DocumentBuilderFactory documentBuilderFactory;

Expand Down
14 changes: 14 additions & 0 deletions src/site/markdown/usage.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,17 @@ writing instructions of another plugin like the `maven-bundle-plugin`:
The `osgi.extender` requirement is mandatory to have the bundle wired up to the Apache Sling Servlets Resolver, but this should be manually
defined by the developers, so that their bundle is correctly wired up to whatever version of the Servlets Resolver is available on the
destination platform.

$h3 Setting a service ranking for the bundled scripts

Starting with version 0.6.0, a numeric `serviceRanking` can be configured. It is applied globally to all scripts in the bundle and
influences the service ranking of the servlets that the Apache Sling Servlets Resolver registers for the bundled scripts. When used as a
bnd plugin, the ranking is written directly into the `Sling-Bundled-Scripts-Ranking` bundle header. When used as a Maven plugin, the value
is exposed via the `org.apache.sling.scriptingbundle.maven.plugin.Sling-Bundled-Scripts-Ranking` project property, which has to be added
to the manifest writing instructions of the plugin that generates the bundle, e.g.:

```
<Sling-Bundled-Scripts-Ranking>
${org.apache.sling.scriptingbundle.maven.plugin.Sling-Bundled-Scripts-Ranking}
</Sling-Bundled-Scripts-Ranking>
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.sling.scriptingbundle.plugin;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

Check warning on line 22 in src/test/java/org/apache/sling/scriptingbundle/plugin/AbstractPluginTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'org.junit.Assert.assertNull'.

See more on https://sonarcloud.io/project/issues?id=apache_sling-scriptingbundle-maven-plugin&issues=AZ_cqf8oO7ixMsVbteRZ&open=AZ_cqf8oO7ixMsVbteRZ&pullRequest=15
import static org.junit.Assert.fail;

import java.util.Arrays;
Expand All @@ -31,6 +32,7 @@
import org.apache.sling.scriptingbundle.plugin.capability.ProvidedResourceTypeCapability;
import org.apache.sling.scriptingbundle.plugin.capability.ProvidedScriptCapability;
import org.apache.sling.scriptingbundle.plugin.capability.RequiredResourceTypeCapability;
import org.apache.sling.scriptingbundle.plugin.processor.Constants;
import org.junit.Test;
import org.osgi.framework.Version;
import org.osgi.framework.VersionRange;
Expand Down Expand Up @@ -158,6 +160,7 @@
PluginExecution execution = executePluginOnProject("project-2");
Capabilities capabilities = execution.getCapabilities();
Map<String, String> scriptEngineMappings = execution.getScriptEngineMappings();
assertEquals("100", execution.getHeaderValue(Constants.SLING_BUNDLED_SCRIPTS_RANKING_HEADER));
Set<ProvidedResourceTypeCapability> pExpected = new HashSet<>(Arrays.asList(
ProvidedResourceTypeCapability.builder().withResourceType("libs/sling/test").withScriptEngine("thymeleaf")
.withScriptExtension("html").build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
package org.apache.sling.scriptingbundle.plugin;

import java.util.Map;
import java.util.function.Function;

import org.apache.sling.scriptingbundle.plugin.capability.Capabilities;

public class PluginExecution {
private final Capabilities capabilities;
private final Map<String, String> scriptEngineMappings;
private final Function<String, String> valueGetter;

public PluginExecution(Capabilities capabilities, Map<String, String> scriptEngineMappings) {
public PluginExecution(Capabilities capabilities, Map<String, String> scriptEngineMappings, Function<String, String> valueGetter) {
super();
this.capabilities = capabilities;
this.scriptEngineMappings = scriptEngineMappings;
this.valueGetter = valueGetter;
}

public Capabilities getCapabilities() {
Expand All @@ -40,4 +43,7 @@ public Map<String, String> getScriptEngineMappings() {
return scriptEngineMappings;
}

public String getHeaderValue(String key) {
return valueGetter.apply(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Function;
import java.util.jar.Manifest;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -66,8 +67,9 @@ public PluginExecution executePluginOnProject(String projectName) throws Excepti
try (Builder builder = prepareBuilder(projectName)) {
BundledScriptsScannerPlugin plugin = builder.getPlugin(BundledScriptsScannerPlugin.class);
assertNotNull(plugin);
builder.build();
return new PluginExecution(plugin.getCapabilities(), plugin.getScriptEngineMappings());
Jar jar = builder.build();
Function<String, String> headerGetter = jar.getManifest().getMainAttributes()::getValue;
return new PluginExecution(plugin.getCapabilities(), plugin.getScriptEngineMappings(), headerGetter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -68,7 +66,9 @@ private static class MojoProject {
public PluginExecution executePluginOnProject(String projectName) throws Exception {
MojoProject mojoProject = getMojoProject(projectName);
mojoProject.mojo.execute();
return new PluginExecution(mojoProject.mojo.getCapabilities(), mojoProject.mojo.getScriptEngineMappings());
Function<String, String> propertyGetter = key -> mojoProject.project.getProperties()
.getProperty("org.apache.sling.scriptingbundle.maven.plugin." + key);
return new PluginExecution(mojoProject.mojo.getCapabilities(), mojoProject.mojo.getScriptEngineMappings(), propertyGetter);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/project-2/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# under the License.
-plugin: org.apache.sling.scriptingbundle.plugin.bnd.BundledScriptsScannerPlugin; \
scriptEngineMappings="html:thymeleaf,js:rhino"; \
searchPaths=/bin
searchPaths=/bin; \
serviceRanking=100
1 change: 1 addition & 0 deletions src/test/resources/project-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<html>thymeleaf</html>
<js>rhino</js>
</scriptEngineMappings>
<serviceRanking>100</serviceRanking>
</configuration>
<executions>
<execution>
Expand Down