From a7d6cf8941699bd0ccd2ca4ba8c7ae14beea8e61 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 17 Dec 2023 12:16:56 +0100 Subject: [PATCH 1/2] [MASSEMBLY-992] Inline Assembly Descriptors --- .../basic-features/inline-descriptor/TODO.txt | 1 + .../inline-descriptor/invoker.properties | 18 ++++ .../basic-features/inline-descriptor/pom.xml | 68 +++++++++++++++ .../inline-descriptor/verify.groovy | 31 +++++++ .../AssemblerConfigurationSource.java | 6 ++ .../assembly/io/DefaultAssemblyReader.java | 5 ++ .../assembly/mojos/AbstractAssemblyMojo.java | 14 +++ src/site/apt/examples/index.apt | 2 + .../examples/using-inline-descriptors.apt.vm | 86 +++++++++++++++++++ src/site/site.xml | 1 + .../io/DefaultAssemblyReaderTest.java | 26 ++++-- .../assembly/testutils/PojoConfigSource.java | 12 +++ 12 files changed, 263 insertions(+), 7 deletions(-) create mode 100644 src/it/projects/basic-features/inline-descriptor/TODO.txt create mode 100644 src/it/projects/basic-features/inline-descriptor/invoker.properties create mode 100644 src/it/projects/basic-features/inline-descriptor/pom.xml create mode 100644 src/it/projects/basic-features/inline-descriptor/verify.groovy create mode 100644 src/site/apt/examples/using-inline-descriptors.apt.vm diff --git a/src/it/projects/basic-features/inline-descriptor/TODO.txt b/src/it/projects/basic-features/inline-descriptor/TODO.txt new file mode 100644 index 000000000..345e6aef7 --- /dev/null +++ b/src/it/projects/basic-features/inline-descriptor/TODO.txt @@ -0,0 +1 @@ +Test diff --git a/src/it/projects/basic-features/inline-descriptor/invoker.properties b/src/it/projects/basic-features/inline-descriptor/invoker.properties new file mode 100644 index 000000000..6e45db42b --- /dev/null +++ b/src/it/projects/basic-features/inline-descriptor/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=org.apache.maven.plugins:maven-assembly-plugin:${project.version}:single diff --git a/src/it/projects/basic-features/inline-descriptor/pom.xml b/src/it/projects/basic-features/inline-descriptor/pom.xml new file mode 100644 index 000000000..64a572e6b --- /dev/null +++ b/src/it/projects/basic-features/inline-descriptor/pom.xml @@ -0,0 +1,68 @@ + + + + + 4.0.0 + + org.apache.maven.plugin.assembly.test + it-project-parent + 1 + + + org.test + inline-descriptor + 1.0 + + + + + org.apache.maven.plugins + maven-assembly-plugin + + false + + + example1 + + dir + + + + TODO.txt + + + + + example2 + + zip + + + + TODO.txt + + + + + + + + + diff --git a/src/it/projects/basic-features/inline-descriptor/verify.groovy b/src/it/projects/basic-features/inline-descriptor/verify.groovy new file mode 100644 index 000000000..7aa19b126 --- /dev/null +++ b/src/it/projects/basic-features/inline-descriptor/verify.groovy @@ -0,0 +1,31 @@ +/* + * 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. + */ + +import java.util.zip.ZipFile + +File todo1 = new File(basedir, 'target/inline-descriptor-1.0-example1/inline-descriptor-1.0/TODO.txt') +assert todo1.exists() + +File zipFile = new File(basedir, 'target/inline-descriptor-1.0-example2.zip') +assert zipFile.exists() + +try (ZipFile zip = new ZipFile(zipFile)) { + assert zip.getEntry('inline-descriptor-1.0/TODO.txt').getSize() > 0 +} + diff --git a/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java b/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java index 8b1a76c2c..5347e88d2 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java +++ b/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java @@ -24,6 +24,7 @@ import org.apache.maven.archiver.MavenArchiveConfiguration; import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.filtering.MavenReaderFilter; import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; @@ -42,6 +43,11 @@ public interface AssemblerConfigurationSource { */ String[] getDescriptorReferences(); + /** + * @return a list of inline descriptors. + */ + List getInlineDescriptors(); + /** * @return The descriptor source directory. */ diff --git a/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java b/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java index c5cefac73..be4af0d1a 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java +++ b/src/main/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReader.java @@ -96,6 +96,7 @@ public List readAssemblies(final AssemblerConfigurationSource configSo final String[] descriptors = configSource.getDescriptors(); final String[] descriptorRefs = configSource.getDescriptorReferences(); final File descriptorSourceDirectory = configSource.getDescriptorSourceDirectory(); + final List inlineDescriptors = configSource.getInlineDescriptors(); if ((descriptors != null) && (descriptors.length > 0)) { locator.setStrategies(strategies); @@ -130,6 +131,10 @@ public List readAssemblies(final AssemblerConfigurationSource configSo } } + if (inlineDescriptors != null) { + assemblies.addAll(inlineDescriptors); + } + if (assemblies.isEmpty()) { if (configSource.isIgnoreMissingDescriptor()) { LOGGER.debug("Ignoring missing assembly descriptors per configuration. " diff --git a/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java b/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java index d81b3056a..814e6f592 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java +++ b/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java @@ -169,6 +169,16 @@ public abstract class AbstractAssemblyMojo extends AbstractMojo implements Assem @Parameter private String[] descriptorRefs; + /** + * An inline list of descriptor to generate from. + *

+ * Each element of list must follow Assembly Descriptor format. + * + * @since 3.7.0 + */ + @Parameter + private List inlineDescriptors; + /** * Directory to scan for descriptor files in. NOTE: This may not work correctly with assembly components. */ @@ -598,6 +608,10 @@ public String[] getDescriptorReferences() { return descriptorRefs; } + public List getInlineDescriptors() { + return inlineDescriptors; + } + @Override public File getDescriptorSourceDirectory() { return descriptorSourceDirectory; diff --git a/src/site/apt/examples/index.apt b/src/site/apt/examples/index.apt index 39a6d28a8..66c51039d 100644 --- a/src/site/apt/examples/index.apt +++ b/src/site/apt/examples/index.apt @@ -55,4 +55,6 @@ Examples * {{{./sharing-descriptors.html}Sharing Assembly Descriptors}} + * {{{./using-inline-descriptors.html}Using Inline Assembly Descriptors}} + [] diff --git a/src/site/apt/examples/using-inline-descriptors.apt.vm b/src/site/apt/examples/using-inline-descriptors.apt.vm new file mode 100644 index 000000000..c6f8c03a3 --- /dev/null +++ b/src/site/apt/examples/using-inline-descriptors.apt.vm @@ -0,0 +1,86 @@ + ------ + Using Inline Assembly Descriptors + ------ + Slawomir Jaranowski + ------ + 2023-12-17 + ------ + +~~ 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. + +~~ NOTE: For help with the syntax of this file, see: +~~ https://maven.apache.org/doxia/references/apt-format.html + +Using Inline Assembly Descriptors + +* Introduction + + For simple usage we can inline Assembly descriptor into project configuration. + We don't need to create additional file with Assembly descriptor. + + It can allow simplify configuration in case of inherited, + we don't need to use a {{{./sharing-descriptors.html}Shared Assembly Descriptors}} + +* The POM + + We can have POM configuration of the project for the Assembly + Plugin, which can look like: + ++----- + + [...] + + [...] + + [...] + + maven-assembly-plugin + ${project.version} + + + + example1 + + dir + + + + TODO.txt + + + + + example2 + + zip + + + + TODO.txt + + + + + + + [...] + ++----- + + + Each element of <<>> must follow {{{../assembly.html}Assembly Descriptor}} format. diff --git a/src/site/site.xml b/src/site/site.xml index 8a9d90d18..0af4fefdd 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -62,6 +62,7 @@ under the License. + diff --git a/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java b/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java index 451c0c292..5f7ea5c96 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/io/DefaultAssemblyReaderTest.java @@ -49,25 +49,27 @@ import org.apache.maven.project.MavenProject; import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; import org.codehaus.plexus.interpolation.fixed.InterpolationState; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.LoggerFactory; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class DefaultAssemblyReaderTest { + @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Mock private AssemblerConfigurationSource configSource; public static StringReader writeToStringReader(Assembly assembly) throws IOException { @@ -79,11 +81,6 @@ public static StringReader writeToStringReader(Assembly assembly) throws IOExcep return new StringReader(sw.toString()); } - @Before - public void setUp() { - configSource = mock(AssemblerConfigurationSource.class); - } - @Test public void testIncludeSiteInAssembly_ShouldFailIfSiteDirectoryNonExistent() throws Exception { final File siteDir = Files.createTempFile("assembly-reader.", ".test").toFile(); @@ -704,6 +701,21 @@ public void testReadAssemblies_ShouldGetTwoAssemblyDescriptorsFromDirectoryWithT assertEquals(assembly2.getId(), result2.getId()); } + @Test + public void inlineAssemblyShouldBeReturned() throws Exception { + // given + Assembly assemby = new Assembly(); + assemby.setId("test-id"); + when(configSource.getInlineDescriptors()).thenReturn(Collections.singletonList(assemby)); + + // when + List assemblies = new DefaultAssemblyReader().readAssemblies(configSource); + + // then + assertEquals(1, assemblies.size()); + assertSame(assemby, assemblies.get(0)); + } + private List writeAssembliesToFile(final List assemblies, final File dir) throws IOException { final List files = new ArrayList<>(); diff --git a/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java b/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java index 468f787c9..dca7bdb32 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java +++ b/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java @@ -26,6 +26,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugins.assembly.AssemblerConfigurationSource; +import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.filtering.MavenReaderFilter; import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator; @@ -115,6 +116,8 @@ public class PojoConfigSource implements AssemblerConfigurationSource { private String mergeManifestMode; + private List inlineDescriptors; + public String getDescriptor() { return descriptor; } @@ -473,4 +476,13 @@ public String getMergeManifestMode() { public void setMergeManifestMode(String mergeManifestMode) { this.mergeManifestMode = mergeManifestMode; } + + @Override + public List getInlineDescriptors() { + return inlineDescriptors; + } + + public void setInlineDescriptors(List inlineDescriptors) { + this.inlineDescriptors = inlineDescriptors; + } } From d23471c4bfc54c817fd2d7016f414c8731e3b91d Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 17 Dec 2023 17:44:53 +0100 Subject: [PATCH 2/2] Fix docs after review --- src/site/apt/examples/using-inline-descriptors.apt.vm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/site/apt/examples/using-inline-descriptors.apt.vm b/src/site/apt/examples/using-inline-descriptors.apt.vm index c6f8c03a3..339621664 100644 --- a/src/site/apt/examples/using-inline-descriptors.apt.vm +++ b/src/site/apt/examples/using-inline-descriptors.apt.vm @@ -30,10 +30,10 @@ Using Inline Assembly Descriptors * Introduction - For simple usage we can inline Assembly descriptor into project configuration. - We don't need to create additional file with Assembly descriptor. + For simple usage we can inline Assembly descriptor into the project configuration. + We don't need to create an additional file with Assembly descriptor. - It can allow simplify configuration in case of inherited, + It can simplify configuration in case of the parent project inherited, we don't need to use a {{{./sharing-descriptors.html}Shared Assembly Descriptors}} * The POM