From 183ce17772f6147179fc5f016a16af5f573ad415 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 21 Dec 2024 13:00:51 +0100 Subject: [PATCH] [MRRESOURCES-154] Allow to require project properties by bundle --- pom.xml | 6 +- .../invoker.properties | 19 +++++++ .../pom.xml | 56 ++++++++++++++++++ .../verify.groovy | 24 ++++++++ .../invoker.properties | 18 ++++++ .../pom.xml | 57 +++++++++++++++++++ .../verify.groovy | 23 ++++++++ src/it/setup-bundle/mrrp-bundle-c/pom.xml | 36 ++++++++++++ .../main/resources/required-property.txt.vm | 18 ++++++ src/it/setup-bundle/pom.xml | 2 + .../AbstractProcessRemoteResourcesMojo.java | 16 ++++++ .../remote/BundleRemoteResourcesMojo.java | 13 ++++- src/main/mdo/remote-resources.mdo | 21 +++++-- src/main/mdo/supplemental-model.mdo | 4 +- 14 files changed, 301 insertions(+), 12 deletions(-) create mode 100644 src/it/process-required-project-properties-missing/invoker.properties create mode 100644 src/it/process-required-project-properties-missing/pom.xml create mode 100644 src/it/process-required-project-properties-missing/verify.groovy create mode 100644 src/it/process-required-project-properties-ok/invoker.properties create mode 100644 src/it/process-required-project-properties-ok/pom.xml create mode 100644 src/it/process-required-project-properties-ok/verify.groovy create mode 100644 src/it/setup-bundle/mrrp-bundle-c/pom.xml create mode 100644 src/it/setup-bundle/mrrp-bundle-c/src/main/resources/required-property.txt.vm diff --git a/pom.xml b/pom.xml index b2b4214..acc83b0 100644 --- a/pom.xml +++ b/pom.xml @@ -281,7 +281,7 @@ under the License. pre-site - 1.1.0 + 1.2.0 ${project.reporting.outputDirectory}/xsd src/main/mdo/remote-resources.mdo @@ -309,7 +309,7 @@ under the License. pre-site - 1.1.0 + 1.2.0 src/main/mdo/remote-resources.mdo @@ -336,7 +336,7 @@ under the License. xpp3-reader - 1.1.0 + 1.2.0 src/main/mdo/remote-resources.mdo diff --git a/src/it/process-required-project-properties-missing/invoker.properties b/src/it/process-required-project-properties-missing/invoker.properties new file mode 100644 index 0000000..7e8906f --- /dev/null +++ b/src/it/process-required-project-properties-missing/invoker.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +invoker.goals = generate-resources +invoker.buildResult = failure diff --git a/src/it/process-required-project-properties-missing/pom.xml b/src/it/process-required-project-properties-missing/pom.xml new file mode 100644 index 0000000..c42bb5b --- /dev/null +++ b/src/it/process-required-project-properties-missing/pom.xml @@ -0,0 +1,56 @@ + + + + + 4.0.0 + + org.apache.maven.its + mrrp-process-required-properties + 1.0-SNAPSHOT + pom + + + UTF-8 + test1 + + + + + + org.apache.maven.plugins + maven-remote-resources-plugin + @project.version@ + + + org.apache.maven.its:mrrp-bundle-c:1.0-SNAPSHOT + + + + + + process + + + + + + + diff --git a/src/it/process-required-project-properties-missing/verify.groovy b/src/it/process-required-project-properties-missing/verify.groovy new file mode 100644 index 0000000..d9629ba --- /dev/null +++ b/src/it/process-required-project-properties-missing/verify.groovy @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +def logs = new File(basedir, 'build.log').text + +assert logs.contains('Required project property: \'testProperty2\' is not present for bundle') + +assert !new File(basedir, 'target/maven-shared-archive-resources').exists() diff --git a/src/it/process-required-project-properties-ok/invoker.properties b/src/it/process-required-project-properties-ok/invoker.properties new file mode 100644 index 0000000..0097f00 --- /dev/null +++ b/src/it/process-required-project-properties-ok/invoker.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +invoker.goals = generate-resources diff --git a/src/it/process-required-project-properties-ok/pom.xml b/src/it/process-required-project-properties-ok/pom.xml new file mode 100644 index 0000000..9cafc8a --- /dev/null +++ b/src/it/process-required-project-properties-ok/pom.xml @@ -0,0 +1,57 @@ + + + + + 4.0.0 + + org.apache.maven.its + mrrp-process-required-properties + 1.0-SNAPSHOT + pom + + + UTF-8 + test1 + test2 + + + + + + org.apache.maven.plugins + maven-remote-resources-plugin + @project.version@ + + + org.apache.maven.its:mrrp-bundle-c:1.0-SNAPSHOT + + + + + + process + + + + + + + diff --git a/src/it/process-required-project-properties-ok/verify.groovy b/src/it/process-required-project-properties-ok/verify.groovy new file mode 100644 index 0000000..ce577bd --- /dev/null +++ b/src/it/process-required-project-properties-ok/verify.groovy @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +def dir = new File(basedir, 'target/maven-shared-archive-resources') + +assert new File(dir, 'required-property.txt').text + .contains('mrrp-bundle-c org.apache.maven.its:mrrp-process-required-properties - test1 - test2') diff --git a/src/it/setup-bundle/mrrp-bundle-c/pom.xml b/src/it/setup-bundle/mrrp-bundle-c/pom.xml new file mode 100644 index 0000000..20a32d0 --- /dev/null +++ b/src/it/setup-bundle/mrrp-bundle-c/pom.xml @@ -0,0 +1,36 @@ + + + + + 4.0.0 + + + org.apache.maven.its + mrrp-bundle-parent + 1.0-SNAPSHOT + ../pom.xml + + + mrrp-bundle-c + + testProperty1,testProperty2 + + diff --git a/src/it/setup-bundle/mrrp-bundle-c/src/main/resources/required-property.txt.vm b/src/it/setup-bundle/mrrp-bundle-c/src/main/resources/required-property.txt.vm new file mode 100644 index 0000000..950bbed --- /dev/null +++ b/src/it/setup-bundle/mrrp-bundle-c/src/main/resources/required-property.txt.vm @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +mrrp-bundle-c ${project.groupId}:${project.artifactId} - ${project.properties.testProperty1} - ${project.properties.testProperty2} diff --git a/src/it/setup-bundle/pom.xml b/src/it/setup-bundle/pom.xml index 2fb7a4a..4ab7261 100644 --- a/src/it/setup-bundle/pom.xml +++ b/src/it/setup-bundle/pom.xml @@ -30,10 +30,12 @@ under the License. mrrp-bundle-a mrrp-bundle-b + mrrp-bundle-c UTF-8 + true diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java index 53fb02a..1aa5255 100644 --- a/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java +++ b/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java @@ -871,6 +871,8 @@ protected void processResourceBundles(ClassLoader classLoader, VelocityContext c bundle = bundleReader.read(in); } + verifyRequiredProperties(bundle, url); + int n = 0; for (String bundleResource : bundle.getRemoteResources()) { n++; @@ -956,6 +958,20 @@ protected void processResourceBundles(ClassLoader classLoader, VelocityContext c } } + private void verifyRequiredProperties(RemoteResourcesBundle bundle, URL url) throws MojoExecutionException { + if (bundle.getRequiredProjectProperties() == null + || bundle.getRequiredProjectProperties().isEmpty()) { + return; + } + + for (String requiredProperty : bundle.getRequiredProjectProperties()) { + if (!project.getProperties().containsKey(requiredProperty)) { + throw new MojoExecutionException( + "Required project property: '" + requiredProperty + "' is not present for bundle: " + url); + } + } + } + private Writer getWriter(RemoteResourcesBundle bundle, File f) throws IOException { Writer writer; if (bundle.getSourceEncoding() == null) { diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java index 591d080..8dedbeb 100644 --- a/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java +++ b/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.Writer; import java.nio.charset.Charset; -import java.util.Arrays; import java.util.Comparator; import java.util.List; @@ -87,6 +86,15 @@ public class BundleRemoteResourcesMojo extends AbstractMojo { @Parameter(defaultValue = "${project.build.sourceEncoding}") private String sourceEncoding; + /** + * List of required project properties needed to process Velocity + * template by this resource bundle. + * + * @since 3.3.0 + */ + @Parameter(property = "bundle.requiredProjectProperties") + private List requiredProjectProperties; + @Override public void execute() throws MojoExecutionException { if (!resourcesDirectory.exists()) { @@ -105,6 +113,7 @@ public void execute() throws MojoExecutionException { RemoteResourcesBundle remoteResourcesBundle = new RemoteResourcesBundle(); remoteResourcesBundle.setSourceEncoding(sourceEncoding); + remoteResourcesBundle.setRequiredProjectProperties(requiredProjectProperties); DirectoryScanner scanner = new DirectoryScanner(); scanner.setFilenameComparator(Comparator.naturalOrder()); @@ -123,7 +132,7 @@ public void execute() throws MojoExecutionException { scanner.addDefaultExcludes(); scanner.scan(); - List includedFiles = Arrays.asList(scanner.getIncludedFiles()); + String[] includedFiles = scanner.getIncludedFiles(); for (String resource : includedFiles) { remoteResourcesBundle.addRemoteResource(StringUtils.replace(resource, '\\', '/')); diff --git a/src/main/mdo/remote-resources.mdo b/src/main/mdo/remote-resources.mdo index b8f63bd..21140c9 100644 --- a/src/main/mdo/remote-resources.mdo +++ b/src/main/mdo/remote-resources.mdo @@ -17,8 +17,8 @@ specific language governing permissions and limitations under the License. --> - remote-resources @@ -36,12 +36,12 @@ under the License. RemoteResourcesBundle - 1.1.0 + 1.1.0+ Root element of the remote-resources.xml file. remoteResources - 1.1.0 + 1.1.0+ @@ -52,12 +52,23 @@ under the License. sourceEncoding - 1.1.0 + 1.1.0+ String + + requiredProjectProperties + 1.2.0+ + + + String + * + + diff --git a/src/main/mdo/supplemental-model.mdo b/src/main/mdo/supplemental-model.mdo index 6b6e74a..475485e 100644 --- a/src/main/mdo/supplemental-model.mdo +++ b/src/main/mdo/supplemental-model.mdo @@ -17,8 +17,8 @@ specific language governing permissions and limitations under the License. --> - supplemental-model