From 78dc92045cd51d458b4140cc1f452fb9240f447a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Thu, 10 Jan 2019 23:50:28 +0100 Subject: [PATCH 01/23] Adding Maven generator for Plugs * Adding tests for Maven plugin * Adding complete test infrastructure for Maven plugin. * Plugin is able to work with DI maven container. Refers to issue #9 --- plugs-core/pom.xml | 4 + plugs-maven-plugin/pom.xml | 46 ++++++++++ .../plugs/maven/generator/DefaultWorker.java | 35 +++++++ .../maven/generator/PackagePlugMojo.java | 57 ++++++++++++ .../plugs/maven/generator/Worker.java | 27 ++++++ .../plugs/maven/generator/package-info.java | 24 +++++ .../generator/DefaultMojoConfigurator.java | 45 +++++++++ .../plugs/maven/generator/MojoBuilder.java | 31 +++++++ .../maven/generator/MojoBuilderFactory.java | 28 ++++++ .../generator/MojoBuilderFactoryImpl.java | 46 ++++++++++ .../maven/generator/MojoBuilderImpl.java | 92 +++++++++++++++++++ .../maven/generator/MojoConfigurator.java | 50 ++++++++++ .../maven/generator/MojoRuleExtension.java | 72 +++++++++++++++ .../maven/generator/PackagePlugMojoTest.java | 67 ++++++++++++++ .../src/test/resources/simpliest/pom.xml | 31 +++++++ pom.xml | 50 ++++++++++ testing/pom.xml | 2 + 17 files changed, 707 insertions(+) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java create mode 100644 plugs-maven-plugin/src/test/resources/simpliest/pom.xml diff --git a/plugs-core/pom.xml b/plugs-core/pom.xml index c5ede20..8777b8e 100644 --- a/plugs-core/pom.xml +++ b/plugs-core/pom.xml @@ -31,6 +31,10 @@ Plugs :: Plugs Core + + org.apiguardian + apiguardian-api + pl.wavesoftware eid-exceptions diff --git a/plugs-maven-plugin/pom.xml b/plugs-maven-plugin/pom.xml index d0d1755..f1901f8 100644 --- a/plugs-maven-plugin/pom.xml +++ b/plugs-maven-plugin/pom.xml @@ -28,5 +28,51 @@ plugs-maven-plugin + maven-plugin Plugs :: Plugs Maven Plugin + + + + org.apiguardian + apiguardian-api + + + org.apache.maven + maven-plugin-api + provided + + + org.apache.maven + maven-core + provided + + + org.apache.maven + maven-compat + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + provided + + + org.apache.maven + maven-settings-builder + provided + + + + + pl.wavesoftware.plugs + testing + ${project.version} + test + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + test + + diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java new file mode 100644 index 0000000..d179cab --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apiguardian.api.API; + +import javax.inject.Named; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@API(status = API.Status.EXPERIMENTAL) +@Named +final class DefaultWorker implements Worker { + + @Override + public String toString() { + return "default"; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java new file mode 100644 index 0000000..0d2f2c4 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apiguardian.api.API; +import org.apiguardian.api.API.Status; + +import javax.inject.Inject; + +/** + * A main mojo to generate plug modules + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Mojo( + name = PackagePlugMojo.GOAL, + defaultPhase = LifecyclePhase.PACKAGE, + requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, + requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME +) +public final class PackagePlugMojo extends AbstractMojo { + + @SuppressWarnings("WeakerAccess") + @API(status = Status.STABLE) + public static final String GOAL = "package-plug"; + + private final Worker worker; + + @Inject + PackagePlugMojo(Worker worker) { + this.worker = worker; + } + + @Override + public void execute() { + getLog().debug("Worker is: " + worker); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java new file mode 100644 index 0000000..c9d9550 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apiguardian.api.API; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@API(status = API.Status.EXPERIMENTAL) +interface Worker { +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java new file mode 100644 index 0000000..0614c5f --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.maven.generator; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java new file mode 100644 index 0000000..e6a32f4 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.project.MavenProject; + +import java.nio.file.Path; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class DefaultMojoConfigurator implements MojoConfigurator { + @Override + public MavenSession getMavenSession(MojoRule rule, Path pomDirectory) throws Exception { + // setup with pom + MavenProject project = rule.readMavenProject(pomDirectory.toFile()); + + // Generate session + return rule.newMavenSession(project); + } + + @Override + public MojoExecution getMojoExecution(MojoRule rule, String goal) { + // Generate Execution and Mojo for testing + return rule.newMojoExecution(goal); + } +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java new file mode 100644 index 0000000..0a0d5b6 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.plugin.AbstractMojo; + +import java.nio.file.Path; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface MojoBuilder { + MojoBuilder withPomDirectory(Path pomDirectory); + MojoBuilder withUsingResources(boolean setting); + T build(String goal); +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java new file mode 100644 index 0000000..4fcade4 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.plugin.AbstractMojo; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface MojoBuilderFactory { + MojoBuilderFactory configurator(MojoConfigurator configurator); + MojoBuilder builder(Class mojoType); +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java new file mode 100644 index 0000000..4f0f6d0 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.testing.MojoRule; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MojoBuilderFactoryImpl implements MojoBuilderFactory { + + private final MojoRule rule; + + private MojoConfigurator configurator = new DefaultMojoConfigurator(); + + MojoBuilderFactoryImpl(MojoRule rule) { + this.rule = rule; + } + + @Override + public MojoBuilderFactory configurator(MojoConfigurator configurator) { + this.configurator = configurator; + return this; + } + + @Override + public MojoBuilder builder(Class mojoType) { + return new MojoBuilderImpl<>(rule, mojoType, configurator); + } +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java new file mode 100644 index 0000000..13ac93f --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.testing.MojoRule; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MojoBuilderImpl + implements MojoBuilder { + + private final MojoRule mojoRule; + private final Class type; + private final MojoConfigurator configurator; + + private Path pomDirectory = Paths.get("."); + private boolean usingResources = true; + + MojoBuilderImpl(MojoRule mojoRule, Class type, MojoConfigurator configurator) { + this.mojoRule = mojoRule; + this.type = type; + this.configurator = configurator; + } + + @Override + public MojoBuilder withPomDirectory(Path pomDirectory) { + this.pomDirectory = pomDirectory; + return this; + } + + @Override + public MojoBuilder withUsingResources(boolean setting) { + this.usingResources = setting; + return this; + } + + @Override + public T build(String goal) { + return tryToExecute(() -> { + MavenSession session = configurator.getMavenSession( + mojoRule, getPomDirectory() + ); + MojoExecution execution = configurator.getMojoExecution( + mojoRule, + goal + ); + org.apache.maven.plugin.Mojo mojo = + mojoRule.lookupConfiguredMojo(session, execution); + return type.cast(mojo); + }, "20190111:223034"); + } + + private Path getPomDirectory() throws URISyntaxException { + if (usingResources) { + URL url = checkNotNull( + type.getClassLoader().getResource(pomDirectory.toString()), + "20190111:224609" + ); + File file = new File(url.toURI()); + return file.toPath(); + } + return pomDirectory; + } +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java new file mode 100644 index 0000000..fef45f9 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.testing.MojoRule; + +import java.nio.file.Path; + +/** + * A configurator for a mojo to be tested + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface MojoConfigurator { + + /** + * Creates a new Maven session + * + * @param rule a mojo rule + * @param pomDirectory a directory of a pom file + * @return a new Maven session + */ + MavenSession getMavenSession(MojoRule rule, Path pomDirectory) throws Exception; + + /** + * Creates a new Mojo execution + * + * @param rule a mojo rule + * @param goal a goal to load mojo for + * @return a new Mojo execution + */ + MojoExecution getMojoExecution(MojoRule rule, String goal); +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java new file mode 100644 index 0000000..e101040 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.plugins.annotations.Mojo; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +import java.lang.reflect.Parameter; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MojoRuleExtension implements BeforeAllCallback, ParameterResolver { + + private static final Namespace MOJO_NS = Namespace.create(Mojo.class); + + @Override + public void beforeAll(ExtensionContext context) throws Exception { + MojoRule rule = new MojoRule(new MojoDelegate()); + context.getStore(MOJO_NS).put(MojoRule.class, rule); + } + + @Override + public boolean supportsParameter( + ParameterContext parameterContext, + ExtensionContext extensionContext + ) throws ParameterResolutionException { + Parameter parameter = parameterContext.getParameter(); + return parameter + .getType() + .isAssignableFrom(MojoBuilderFactory.class); + } + + @Override + public Object resolveParameter( + ParameterContext parameterContext, + ExtensionContext extensionContext + ) throws ParameterResolutionException { + MojoRule mojoRule = extensionContext + .getStore(MOJO_NS) + .get(MojoRule.class, MojoRule.class); + return new MojoBuilderFactoryImpl(mojoRule); + } + + private static final class MojoDelegate extends AbstractMojoTestCase { + MojoDelegate() throws Exception { + setUp(); + } + } +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java new file mode 100644 index 0000000..8898c4b --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.plugin.logging.Log; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith({ + MojoRuleExtension.class, + MockitoExtension.class +}) +class PackagePlugMojoTest { + + @Mock + private Log log; + + @AfterEach + void after() { + Mockito.verifyNoMoreInteractions(log); + Mockito.validateMockitoUsage(); + } + + @Test + void execute(MojoBuilderFactory factory) { + // given + Path pomDirectory = Paths.get("simpliest"); + PackagePlugMojo mojo = factory + .builder(PackagePlugMojo.class) + .withPomDirectory(pomDirectory) + .build(PackagePlugMojo.GOAL); + mojo.setLog(log); + + // when & then + assertThatCode(mojo::execute).doesNotThrowAnyException(); + verify(log).debug(eq("Worker is: default")); + } +} diff --git a/plugs-maven-plugin/src/test/resources/simpliest/pom.xml b/plugs-maven-plugin/src/test/resources/simpliest/pom.xml new file mode 100644 index 0000000..1ae88c9 --- /dev/null +++ b/plugs-maven-plugin/src/test/resources/simpliest/pom.xml @@ -0,0 +1,31 @@ + + + + 4.0.0 + org.example + simpliest + 0.1.0 + + + + + pl.wavesoftware.plugs + plugs-maven-plugin + + + + diff --git a/pom.xml b/pom.xml index 9e5865e..3d25334 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,10 @@ + + 3.0 + + plug-api plugs-core @@ -110,6 +114,7 @@ 2.1.1.RELEASE 6.0.1 + 3.6.0 @@ -121,6 +126,11 @@ pom import + + org.apiguardian + apiguardian-api + 1.0.0 + org.apache.felix org.apache.felix.framework @@ -151,15 +161,55 @@ eid-exceptions 2.0.0 + + org.apache.maven + maven-plugin-api + ${maven.version} + provided + + + org.apache.maven + maven-core + ${maven.version} + provided + + + org.apache.maven + maven-compat + ${maven.version} + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${maven.version} + provided + + + org.apache.maven + maven-settings-builder + ${maven.version} + provided + + + org.awaitility awaitility 3.1.5 + test com.portingle slf4jtesting 1.1.3 + test + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + 3.3.0 + test diff --git a/testing/pom.xml b/testing/pom.xml index 726ee27..f6d1e62 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -50,6 +50,7 @@ com.portingle slf4jtesting + runtime org.junit.platform @@ -74,6 +75,7 @@ org.awaitility awaitility + runtime io.vavr From 9f7bb73769b0fb51c2c909eeb98fe1fde0112798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Sun, 13 Jan 2019 22:09:44 +0100 Subject: [PATCH 02/23] Move mojo extension to separate package --- .../junit5}/DefaultMojoConfigurator.java | 2 +- .../maven/junit5/MojoBuilder.java | 53 +++++++++++++++++++ .../junit5}/MojoBuilderFactory.java | 2 +- .../junit5}/MojoBuilderFactoryImpl.java | 2 +- .../junit5}/MojoBuilderImpl.java | 2 +- .../junit5}/MojoConfigurator.java | 2 +- .../junit5}/MojoRuleExtension.java | 5 +- .../junit5/package-info.java} | 15 ++---- .../maven/generator/PackagePlugMojoTest.java | 2 + 9 files changed, 67 insertions(+), 18 deletions(-) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator => maven/junit5}/DefaultMojoConfigurator.java (96%) create mode 100644 plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator => maven/junit5}/MojoBuilderFactory.java (95%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator => maven/junit5}/MojoBuilderFactoryImpl.java (96%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator => maven/junit5}/MojoBuilderImpl.java (98%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator => maven/junit5}/MojoConfigurator.java (96%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator => maven/junit5}/MojoRuleExtension.java (94%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/{plugs/maven/generator/MojoBuilder.java => maven/junit5/package-info.java} (69%) diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java similarity index 96% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java index e6a32f4..81975ef 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/DefaultMojoConfigurator.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.maven.junit5; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecution; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java new file mode 100644 index 0000000..6824767 --- /dev/null +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.maven.junit5; + +import org.apache.maven.plugin.AbstractMojo; + +import java.nio.file.Path; + +/** + * Builds mojo while configuring it with builder like interface + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface MojoBuilder { + /** + * Sets a POM directory to be used + * + * @param pomDirectory a pom directory + * @return self reference + */ + MojoBuilder withPomDirectory(Path pomDirectory); + + /** + * Sets wherever to use Java resources of file system path + * + * @param setting a setting of resources location + * @return self reference + */ + MojoBuilder withUsingResources(boolean setting); + + /** + * Builds a Mojo for a given goal + * + * @param goal a goal to retrieve mojo for + * @return a mojo + */ + T build(String goal); +} diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java similarity index 95% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java index 4fcade4..c75ebaa 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactory.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.maven.junit5; import org.apache.maven.plugin.AbstractMojo; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java similarity index 96% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java index 4f0f6d0..a9618e5 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderFactoryImpl.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.maven.junit5; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.testing.MojoRule; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderImpl.java similarity index 98% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderImpl.java index 13ac93f..560168b 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilderImpl.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.maven.junit5; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoConfigurator.java similarity index 96% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoConfigurator.java index fef45f9..2390579 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoConfigurator.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoConfigurator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.maven.junit5; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecution; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java similarity index 94% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java index e101040..58a59ea 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoRuleExtension.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.maven.junit5; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.MojoRule; @@ -32,7 +32,8 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -final class MojoRuleExtension implements BeforeAllCallback, ParameterResolver { +public final class MojoRuleExtension + implements BeforeAllCallback, ParameterResolver { private static final Namespace MOJO_NS = Namespace.create(Mojo.class); diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/package-info.java similarity index 69% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java rename to plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/package-info.java index 0a0d5b6..a0442f3 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/MojoBuilder.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/package-info.java @@ -14,18 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; - -import org.apache.maven.plugin.AbstractMojo; - -import java.nio.file.Path; - /** * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface MojoBuilder { - MojoBuilder withPomDirectory(Path pomDirectory); - MojoBuilder withUsingResources(boolean setting); - T build(String goal); -} +@ParametersAreNonnullByDefault +package pl.wavesoftware.maven.junit5; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java index 8898c4b..175afdc 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java @@ -23,6 +23,8 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import pl.wavesoftware.maven.junit5.MojoBuilderFactory; +import pl.wavesoftware.maven.junit5.MojoRuleExtension; import java.nio.file.Path; import java.nio.file.Paths; From 000871774388699ab8c27db7d955353b5cd24610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Sun, 13 Jan 2019 23:13:29 +0100 Subject: [PATCH 03/23] Adding basic architecture of Maven plugin --- plugs-maven-plugin/pom.xml | 17 +- .../DefaultExecutionConfiguration.java | 75 +++++++ .../maven/generator/PackagePlugMojo.java | 191 +++++++++++++++++- .../filter/AbstractDependencyFilter.java | 95 +++++++++ .../maven/generator/filter/DefaultFilter.java | 49 +++++ .../filter/DefaultFilterFactory.java | 56 +++++ .../maven/generator/filter/ExcludeFilter.java | 49 +++++ .../plugs/maven/generator/filter/Filter.java | 33 +++ .../maven/generator/filter/FilterFactory.java | 36 ++++ .../maven/generator/filter/IncludeFilter.java | 49 +++++ .../maven/generator/filter/package-info.java | 24 +++ .../model/AbstractFilterableDependency.java | 83 ++++++++ .../{Worker.java => model/BuildFailure.java} | 8 +- .../maven/generator/model/BuildResult.java | 26 +++ .../plugs/maven/generator/model/Exclude.java | 25 +++ .../model/ExecutionConfiguration.java | 33 +++ .../generator/model/FilterableDependency.java | 30 +++ .../Include.java} | 15 +- .../maven/generator/model/Libraries.java | 43 ++++ .../plugs/maven/generator/model/Library.java | 102 ++++++++++ .../generator/model/LibraryCallback.java | 37 ++++ .../maven/generator/model/LibraryScope.java | 47 +++++ .../generator/model/PlugsMojoException.java | 49 +++++ .../maven/generator/model/package-info.java | 24 +++ .../generator/packager/PlugPackager.java | 60 ++++++ .../packager/PlugPackagerFactory.java | 28 +++ .../packager/PlugPackagerFactoryImpl.java | 34 ++++ .../generator/packager/PlugPackagerImpl.java | 118 +++++++++++ .../generator/packager/package-info.java | 24 +++ .../maven/junit5/DefaultMojoConfigurator.java | 2 +- .../maven/generator/PackagePlugMojoTest.java | 2 +- pom.xml | 5 + 32 files changed, 1440 insertions(+), 29 deletions(-) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/{Worker.java => model/BuildFailure.java} (83%) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/{DefaultWorker.java => model/Include.java} (72%) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java diff --git a/plugs-maven-plugin/pom.xml b/plugs-maven-plugin/pom.xml index f1901f8..35fdf62 100644 --- a/plugs-maven-plugin/pom.xml +++ b/plugs-maven-plugin/pom.xml @@ -32,10 +32,6 @@ Plugs :: Plugs Maven Plugin - - org.apiguardian - apiguardian-api - org.apache.maven maven-plugin-api @@ -62,6 +58,19 @@ provided + + org.apiguardian + apiguardian-api + + + org.apache.maven.shared + maven-common-artifact-filters + + + io.vavr + vavr + + pl.wavesoftware.plugs diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java new file mode 100644 index 0000000..72e1049 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator; + +import org.apache.maven.project.MavenProject; +import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; + +import java.io.File; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class DefaultExecutionConfiguration + implements ExecutionConfiguration { + + private final MavenProject project; + private final String classifier; + private final boolean attach; + private final File outputDirectory; + private final String finalName; + + DefaultExecutionConfiguration( + MavenProject project, + String classifier, + boolean attach, + File outputDirectory, + String finalName + ) { + this.project = project; + this.classifier = classifier; + this.attach = attach; + this.outputDirectory = outputDirectory; + this.finalName = finalName; + } + + @Override + public String getFinalName() { + return finalName; + } + + @Override + public String getClassifier() { + return classifier; + } + + @Override + public boolean shouldAttach() { + return attach; + } + + @Override + public File getOutputDirectory() { + return outputDirectory; + } + + @Override + public MavenProject getMavenProject() { + return project; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java index 0d2f2c4..c1738b9 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java @@ -19,11 +19,25 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; import org.apiguardian.api.API; import org.apiguardian.api.API.Status; +import pl.wavesoftware.plugs.maven.generator.filter.Filter; +import pl.wavesoftware.plugs.maven.generator.filter.FilterFactory; +import pl.wavesoftware.plugs.maven.generator.model.BuildFailure; +import pl.wavesoftware.plugs.maven.generator.model.Exclude; +import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import pl.wavesoftware.plugs.maven.generator.model.Include; +import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; +import pl.wavesoftware.plugs.maven.generator.packager.PlugPackager; +import pl.wavesoftware.plugs.maven.generator.packager.PlugPackagerFactory; import javax.inject.Inject; +import java.io.File; +import java.util.List; /** * A main mojo to generate plug modules @@ -41,17 +55,182 @@ public final class PackagePlugMojo extends AbstractMojo { @SuppressWarnings("WeakerAccess") @API(status = Status.STABLE) - public static final String GOAL = "package-plug"; + public static final String GOAL = "package"; - private final Worker worker; + /** + * The package factory + */ + private final PlugPackagerFactory packagerFactory; + + /** + * The filter factory + */ + private final FilterFactory filterFactory; + + /** + * Maven's project helper + */ + private final MavenProjectHelper projectHelper; + + /** + * The Maven project. + * + * @since 0.1.0 + */ + @Parameter( + defaultValue = "${project}", + required = true, + readonly = true + ) + private MavenProject project; + + /** + * Classifier to add to the repackaged archive. If not given, classifier + * "plug" will be used. If given, the classifier will also be used + * to determine the source archive to repackage: if an artifact with that + * classifier already exists, it will be used as source and replaced. If no + * such artifact exists, the main artifact will be used as source and the + * repackaged archive will be attached as a supplemental artifact with that + * classifier. Attaching the artifact allows to deploy it alongside to the + * original one, see the maven documentation for more details. + * + * @since 0.1.0 + */ + @Parameter(property = "plugs.classifier", defaultValue = "plug") + private String classifier = "plug"; + + /** + * Attach the repackaged archive to be installed and deployed. + * + * @since 0.1.0 + */ + @Parameter(property = "plugs.attach", defaultValue = "true") + private boolean attach = true; + + /** + * Directory containing the generated archive. + * + * @since 0.1.0 + */ + @Parameter(defaultValue = "${project.build.directory}", required = true) + private File outputDirectory; + + /** + * Name of the generated archive. + * + * @since 0.1.0 + */ + @Parameter(defaultValue = "${project.build.finalName}", readonly = true) + private String finalName; + + /** + * Skip the execution. + * + * @since 0.1.0 + */ + @Parameter(property = "plugs.skip", defaultValue = "false") + private boolean skip; + + /** + * Collection of artifact definitions to include. The {@link Include} element + * defines a {@code groupId} and {@code artifactId} mandatory properties + * and an optional {@code classifier} property. + * + * @since 0.1.0 + */ + @Parameter(property = "plugs.includes") + private List includes; + + /** + * Collection of artifact definitions to exclude. The {@link Exclude} element + * defines a {@code groupId} and {@code artifactId} mandatory properties + * and an optional {@code classifier} property. + * + * @since 0.1.0 + */ + @Parameter(property = "plugs.excludes") + private List excludes; @Inject - PackagePlugMojo(Worker worker) { - this.worker = worker; + PackagePlugMojo( + PlugPackagerFactory packagerFactory, + FilterFactory filterFactory, + MavenProjectHelper projectHelper + ) { + this.packagerFactory = packagerFactory; + this.filterFactory = filterFactory; + this.projectHelper = projectHelper; } @Override - public void execute() { - getLog().debug("Worker is: " + worker); + public void execute() throws PlugsMojoException { + if ("pom".equals(project.getPackaging())) { + getLog().debug(GOAL + " goal could not be applied to pom project."); + return; + } + if (this.skip) { + getLog().debug("skipping packaging as per configuration."); + return; + } + build(); + } + + private void build() throws PlugsMojoException { + Filter filter = filterFactory.create(includes, excludes); + ExecutionConfiguration configuration = getConfiguration(); + PlugPackager packager = packagerFactory.create(configuration, filter); + if (packager.needsRepackaging()) { + buildAndReport(packager, configuration); + } else { + getLog().info("Plug package " + finalName + " is up-to-date."); + } + } + + private void buildAndReport( + PlugPackager packager, + ExecutionConfiguration configuration + ) throws PlugsMojoException { + try { + packager.repackageAsPlug(); + getLog().info("Building of " + finalName + " was successful."); + attachIfNeeded(packager, configuration); + } catch (PlugsMojoException ex) { + getLog().error("Building of " + finalName + " has failed."); + ex.getFailures().ifPresent(failures -> { + getLog().error("Reasons of failure:"); + for (BuildFailure failure : failures) { + getLog().error(" * " + failure.describe()); + } + }); + throw ex; + } + } + + private void attachIfNeeded( + PlugPackager packager, + ExecutionConfiguration configuration + ) { + if (configuration.shouldAttach()) { + projectHelper.attachArtifact( + project, + packager.getTargetFile(), + configuration.getClassifier() + ); + getLog().info("Artifact attached to the build: " + packager.getTargetFile()); + } else { + getLog().debug("Skipping attach of artifact: " + packager.getTargetFile()); + } + } + + private ExecutionConfiguration getConfiguration() { + return new DefaultExecutionConfiguration( + project, + classifier, + attach, + outputDirectory, + finalName + ); } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java new file mode 100644 index 0000000..cc52257 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter; +import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; +import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; +import pl.wavesoftware.plugs.maven.generator.model.FilterableDependency; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Base class for {@link ArtifactsFilter} based on a {@link FilterableDependency} list. + * + * @author Krzysztof Suszynski + * @author Stephane Nicoll (Spring Boot project) + * @author David Turanski (Spring Boot project) + * @since 0.1.0 + */ +abstract class AbstractDependencyFilter extends AbstractArtifactsFilter { + private final List filters; + + /** + * Create a new instance with the list of {@link FilterableDependency} instance(s) to + * use. + * + * @param dependencies the source dependencies + */ + AbstractDependencyFilter(List dependencies) { + this.filters = new ArrayList<>(dependencies); + } + + @Override + public Set filter(Set artifacts) throws ArtifactFilterException { + Set result = new HashSet<>(); + for (Artifact artifact : artifacts) { + if (!filter(artifact)) { + result.add(artifact); + } + } + return Collections.unmodifiableSet(result); + } + + protected abstract boolean filter(Artifact artifact) throws ArtifactFilterException; + + /** + * Check if the specified {@link org.apache.maven.artifact.Artifact} matches the + * specified {@link FilterableDependency}. Returns + * {@code true} if it should be excluded + * + * @param artifact the Maven {@link Artifact} + * @param dependency the {@link FilterableDependency} + * @return {@code true} if the artifact matches the dependency + */ + final boolean equals( + Artifact artifact, + FilterableDependency dependency + ) { + if (!dependency.getGroupId().equals(artifact.getGroupId())) { + return false; + } + if (!dependency.getArtifactId().equals(artifact.getArtifactId())) { + return false; + } + return (dependency.getClassifier() == null + || ( + artifact.getClassifier() != null + && dependency.getClassifier().equals(artifact.getClassifier()) + ) + ); + } + + final List getFilters() { + return Collections.unmodifiableList(filters); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java new file mode 100644 index 0000000..bd9b48a --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; +import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; +import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; + +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class DefaultFilter implements Filter { + private final FilterArtifacts filters; + + DefaultFilter(FilterArtifacts filters) { + this.filters = filters; + } + + @Override + public Set filterDependencies(Set dependencies) + throws PlugsMojoException { + try { + Set filtered = new LinkedHashSet<>(dependencies); + filtered.retainAll(filters.filter(dependencies)); + return filtered; + } catch (ArtifactFilterException ex) { + throw new PlugsMojoException(ex.getMessage(), ex); + } + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java new file mode 100644 index 0000000..dfc9d95 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; +import pl.wavesoftware.plugs.maven.generator.model.Exclude; +import pl.wavesoftware.plugs.maven.generator.model.Include; + +import javax.annotation.Nullable; +import javax.inject.Named; +import java.util.List; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class DefaultFilterFactory implements FilterFactory { + @Override + public Filter create( + @Nullable List includes, + @Nullable List excludes + ) { + FilterArtifacts filters = getFilters(includes, excludes); + return new DefaultFilter(filters); + } + + private static FilterArtifacts getFilters( + @Nullable List includes, + @Nullable List excludes + ) { + FilterArtifacts filters = new FilterArtifacts(); + if (includes != null && !includes.isEmpty()) { + filters.addFilter(new IncludeFilter(includes)); + } + if (excludes != null && !excludes.isEmpty()) { + filters.addFilter(new ExcludeFilter(excludes)); + } + + return filters; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java new file mode 100644 index 0000000..d7c0aa3 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import org.apache.maven.artifact.Artifact; +import pl.wavesoftware.plugs.maven.generator.model.Exclude; +import pl.wavesoftware.plugs.maven.generator.model.FilterableDependency; + +import java.util.List; + +/** + * An {DependencyFilter} that filters out any artifact matching an {@link Exclude}. + * + * @author Krzysztof Suszynski + * @author Stephane Nicoll (Spring Boot project) + * @author David Turanski (Spring Boot project) + * @since 0.1.0 + */ +final class ExcludeFilter extends AbstractDependencyFilter { + + ExcludeFilter(List excludes) { + super(excludes); + } + + @Override + protected boolean filter(Artifact artifact) { + for (FilterableDependency dependency : getFilters()) { + if (equals(artifact, dependency)) { + return true; + } + } + return false; + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java new file mode 100644 index 0000000..7701e88 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import org.apache.maven.artifact.Artifact; +import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; + +import java.util.Set; + +/** + * Filters dependencies according to the rules of a filter. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface Filter { + Set filterDependencies(Set dependencies) + throws PlugsMojoException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java new file mode 100644 index 0000000..c7fc6ca --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import pl.wavesoftware.plugs.maven.generator.model.Exclude; +import pl.wavesoftware.plugs.maven.generator.model.Include; + +import javax.annotation.Nullable; +import java.util.List; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface FilterFactory { + Filter create( + @Nullable + List includes, + @Nullable + List excludes + ); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java new file mode 100644 index 0000000..60cf56d --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.filter; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; +import pl.wavesoftware.plugs.maven.generator.model.FilterableDependency; +import pl.wavesoftware.plugs.maven.generator.model.Include; + +import java.util.List; + +/** + * An {@link ArtifactsFilter} that filters out any artifact not matching an + * {@link Include}. + * + * @author Krzysztof Suszynski + * @author David Turanski (Spring Boot project) + * @since 0.1.0 + */ +final class IncludeFilter extends AbstractDependencyFilter { + + IncludeFilter(List includes) { + super(includes); + } + + @Override + protected boolean filter(Artifact artifact) { + for (FilterableDependency dependency : getFilters()) { + if (equals(artifact, dependency)) { + return false; + } + } + return true; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java new file mode 100644 index 0000000..67f15d1 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.maven.generator.filter; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java new file mode 100644 index 0000000..8e9f0fb --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import org.apache.maven.plugins.annotations.Parameter; + +/** + * @author Krzysztof Suszynski + * @author Stephane Nicoll (Spring Boot project) + * @author David Turanski (Spring Boot project) + * @since 0.1.0 + */ +abstract class AbstractFilterableDependency + implements FilterableDependency { + + /** + * The groupId of the artifact to exclude. + * + * @since 0.1.0 + */ + @Parameter(required = true) + private String groupId; + + /** + * The artifactId of the artifact to exclude. + * + * @since 0.1.0 + */ + @Parameter(required = true) + private String artifactId; + + /** + * The classifier of the artifact to exclude. + * + * @since 0.1.0 + */ + @Parameter + private String classifier; + + @Override + public String getGroupId() { + return groupId; + } + + @Override + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + @Override + public String getArtifactId() { + return artifactId; + } + + @Override + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + @Override + public String getClassifier() { + return classifier; + } + + @Override + public void setClassifier(String classifier) { + this.classifier = classifier; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java similarity index 83% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java rename to plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java index c9d9550..f4fcea2 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/Worker.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java @@ -14,14 +14,12 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; - -import org.apiguardian.api.API; +package pl.wavesoftware.plugs.maven.generator.model; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -@API(status = API.Status.EXPERIMENTAL) -interface Worker { +public interface BuildFailure { + String describe(); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java new file mode 100644 index 0000000..de91ae1 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface BuildResult { + boolean isSuccessful(); + Iterable getFailures(); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java new file mode 100644 index 0000000..89abbe4 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +/** + * @author Krzysztof Suszynski + * @author Stephane Nicoll (Spring Boot project) + * @since 0.1.0 + */ +public final class Exclude extends AbstractFilterableDependency { +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java new file mode 100644 index 0000000..86f38e6 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import org.apache.maven.project.MavenProject; + +import java.io.File; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface ExecutionConfiguration { + String getFinalName(); + String getClassifier(); + boolean shouldAttach(); + File getOutputDirectory(); + MavenProject getMavenProject(); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java new file mode 100644 index 0000000..9f78466 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface FilterableDependency { + String getGroupId(); + void setGroupId(String groupId); + String getArtifactId(); + void setArtifactId(String artifactId); + String getClassifier(); + void setClassifier(String classifier); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Include.java similarity index 72% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java rename to plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Include.java index d179cab..d45eeb4 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultWorker.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Include.java @@ -14,22 +14,13 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; - -import org.apiguardian.api.API; - -import javax.inject.Named; +package pl.wavesoftware.plugs.maven.generator.model; /** * @author Krzysztof Suszynski + * @author David Turanski (Spring Boot project) * @since 0.1.0 */ -@API(status = API.Status.EXPERIMENTAL) -@Named -final class DefaultWorker implements Worker { +public final class Include extends AbstractFilterableDependency { - @Override - public String toString() { - return "default"; - } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java new file mode 100644 index 0000000..84d5309 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import java.io.IOException; + +/** + * Encapsulates information about libraries that may be packed into the archive. + * + * @author Krzysztof Suszynski + * @author Phillip Webb (Spring Boot project) + * @since 0.1.0 + */ +@FunctionalInterface +public interface Libraries { + + /** + * Represents no libraries. + */ + Libraries NONE = (callback) -> { + }; + + /** + * Iterate all relevant libraries. + * @param callback a callback for each relevant library. + * @throws IOException if the operation fails + */ + void doWithLibraries(LibraryCallback callback) throws IOException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java new file mode 100644 index 0000000..9d87f83 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import java.io.File; + +/** + * Encapsulates information about a single library that may be packed into + * the archive. + * + * @author Krzysztof Suszynski + * @author Phillip Webb (Spring Boot project) + * @since 0.1.0 + * @see Libraries + */ +public final class Library { + private final String name; + private final File file; + private final LibraryScope scope; + private final boolean unpackRequired; + + /** + * Create a new {@link Library}. + * @param file the source file + * @param scope the scope of the library + */ + public Library(File file, LibraryScope scope) { + this(file, scope, false); + } + + /** + * Create a new {@link Library}. + * @param file the source file + * @param scope the scope of the library + * @param unpackRequired if the library needs to be unpacked before it can be used + */ + public Library(File file, LibraryScope scope, boolean unpackRequired) { + this(null, file, scope, unpackRequired); + } + + /** + * Create a new {@link Library}. + * @param name the name of the library as it should be written or {@code null} to use + * the file name + * @param file the source file + * @param scope the scope of the library + * @param unpackRequired if the library needs to be unpacked before it can be used + */ + public Library(String name, File file, LibraryScope scope, boolean unpackRequired) { + this.name = (name != null) ? name : file.getName(); + this.file = file; + this.scope = scope; + this.unpackRequired = unpackRequired; + } + + /** + * Return the name of file as it should be written. + * @return the name + */ + public String getName() { + return this.name; + } + + /** + * Return the library file. + * @return the file + */ + public File getFile() { + return this.file; + } + + /** + * Return the scope of the library. + * @return the scope + */ + public LibraryScope getScope() { + return this.scope; + } + + /** + * Return if the file cannot be used directly as a nested jar and needs to be + * unpacked. + * @return if unpack is required + */ + public boolean isUnpackRequired() { + return this.unpackRequired; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java new file mode 100644 index 0000000..1a1dee5 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import java.io.File; +import java.io.IOException; + +/** + * Callback interface used to iterate {@link Libraries}. + * + * @author Krzysztof Suszynski + * @author Phillip Webb (Spring Boot project) + * @since 0.1.0 + */ +@FunctionalInterface +public interface LibraryCallback { + /** + * Callback for a single library backed by a {@link File}. + * @param library the library + * @throws IOException if the operation fails + */ + void library(Library library) throws IOException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java new file mode 100644 index 0000000..b174dd9 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +/** + * The scope of a library. The common {@link #COMPILE}, {@link #RUNTIME} and + * {@link #PROVIDED} scopes are defined here and supported by the common + * {@link Layouts}. + * + * A custom {@link Layout} can handle additional scopes as required. + * + * @author Krzysztof Suszynski + * @author Phillip Webb (Spring Boot project) + * @since 0.1.0 + */ +public enum LibraryScope { + + COMPILE("compile"), + RUNTIME("runtime"), + PROVIDED("provided"), + CUSTOM("custom"); + + private final String name; + + LibraryScope(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java new file mode 100644 index 0000000..6c19813 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import org.apache.maven.plugin.MojoExecutionException; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * A standard Plugs mojo exception + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public final class PlugsMojoException extends MojoExecutionException { + @Nullable + private final ArrayList failures; + + public PlugsMojoException(List failures) { + super("Multiple reasons of failure"); + this.failures = new ArrayList<>(failures); + } + + public PlugsMojoException(String message, Throwable cause) { + super(message, cause); + failures = null; + } + + public Optional> getFailures() { + return Optional.ofNullable(failures); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java new file mode 100644 index 0000000..a1d8ac5 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.maven.generator.model; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java new file mode 100644 index 0000000..3512383 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import org.apache.maven.artifact.Artifact; +import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; + +import java.io.File; + +/** + * A Plugs packager that repackage jar as a plug module. All dependencies will + * be embedded as a internal dependencies. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface PlugPackager { + /** + * Checks where repackage action is required + * + * @return true, if repackage is needed + */ + boolean needsRepackaging(); + + /** + * Repackages a artifact as a Plug module + * + * @throws PlugsMojoException if something goes wrong in repackage process + */ + void repackageAsPlug() throws PlugsMojoException; + + /** + * Gets a source artifact that will be a base for re-packaging as a Plug + * module. + * + * @return a source artifact + */ + Artifact getSourceArtifact(); + + /** + * Gets a target file to be created during packaging process. + * + * @return a target file to be repackaged to + */ + File getTargetFile(); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java new file mode 100644 index 0000000..6fef091 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.filter.Filter; +import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface PlugPackagerFactory { + PlugPackager create(ExecutionConfiguration configuration, Filter filter); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java new file mode 100644 index 0000000..153608c --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.filter.Filter; +import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; + +import javax.inject.Named; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class PlugPackagerFactoryImpl implements PlugPackagerFactory { + @Override + public PlugPackager create(ExecutionConfiguration configuration, Filter filter) { + return new PlugPackagerImpl(configuration, filter); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java new file mode 100644 index 0000000..ed2663f --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import io.vavr.Lazy; +import org.apache.maven.artifact.Artifact; +import pl.wavesoftware.plugs.maven.generator.filter.Filter; +import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; + +import javax.annotation.Nullable; +import java.io.File; +import java.util.Set; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class PlugPackagerImpl implements PlugPackager { + + private final ExecutionConfiguration configuration; + private final Filter filter; + private final Lazy lazyTargetFile; + private final Lazy lazySourceArtifact; + + PlugPackagerImpl(ExecutionConfiguration configuration, Filter filter) { + this.configuration = configuration; + this.filter = filter; + lazyTargetFile = Lazy.of(this::calculateTargetFile); + lazySourceArtifact = Lazy.of(this::calculateSourceArtifact); + } + + @Override + public boolean needsRepackaging() { + return true; + } + + @Override + public void repackageAsPlug() throws PlugsMojoException { + Artifact source = getSourceArtifact(); + File target = getTargetFile(); + Set artifacts = filter.filterDependencies( + configuration.getMavenProject().getArtifacts() + ); + + repackage(source, target, artifacts); + } + + @Override + public File getTargetFile() { + return lazyTargetFile.get(); + } + + @Override + public Artifact getSourceArtifact() { + return lazySourceArtifact.get(); + } + + private void repackage( + Artifact source, + File target, + Set artifacts + ) { + throw new UnsupportedOperationException("Not yet implemented"); + } + + private Artifact calculateSourceArtifact() { + @Nullable + Artifact sourceArtifact = getArtifact(configuration.getClassifier()); + return (sourceArtifact != null) + ? sourceArtifact + : configuration.getMavenProject().getArtifact(); + } + + private File calculateTargetFile() { + String classifier = configuration.getClassifier().trim(); + if (!classifier.isEmpty() && !classifier.startsWith("-")) { + classifier = "-" + classifier; + } + assert configuration.getOutputDirectory().exists() + || configuration.getOutputDirectory().mkdirs(); + return new File( + configuration.getOutputDirectory(), + configuration.getFinalName() + + classifier + "." + + configuration.getMavenProject() + .getArtifact() + .getArtifactHandler() + .getExtension() + ); + } + + @Nullable + private Artifact getArtifact(String classifier) { + for (Artifact attachedArtifact : configuration.getMavenProject().getAttachedArtifacts()) { + if (classifier.equals(attachedArtifact.getClassifier()) + && attachedArtifact.getFile() != null + && attachedArtifact.getFile().isFile()) { + return attachedArtifact; + } + } + return null; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java new file mode 100644 index 0000000..f5db80d --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.maven.generator.packager; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java index 81975ef..3c29538 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java @@ -39,7 +39,7 @@ public MavenSession getMavenSession(MojoRule rule, Path pomDirectory) throws Exc @Override public MojoExecution getMojoExecution(MojoRule rule, String goal) { - // Generate Execution and Mojo for testing + // Generate ExecutionConfiguration and Mojo for testing return rule.newMojoExecution(goal); } } diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java index 175afdc..f538abc 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java @@ -64,6 +64,6 @@ void execute(MojoBuilderFactory factory) { // when & then assertThatCode(mojo::execute).doesNotThrowAnyException(); - verify(log).debug(eq("Worker is: default")); + verify(log).debug(eq("Packager is: default, project: simpliest-0.1.0")); } } diff --git a/pom.xml b/pom.xml index 3d25334..78916b1 100644 --- a/pom.xml +++ b/pom.xml @@ -179,6 +179,11 @@ ${maven.version} provided + + org.apache.maven.shared + maven-common-artifact-filters + 3.1.0 + org.apache.maven.plugin-tools maven-plugin-annotations From 7ad30412fcc17cd4dd5caf9689766045070b64a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Wed, 16 Jan 2019 00:34:17 +0100 Subject: [PATCH 04/23] Adding most of basic packager logic A basic Packager logic has been added. Most of the code has been inspired by Spring Boot Maven plugin, but code has been strimlined to suit simplier needs of building just bundle jars. Packager logic should be rewriten so it will package sources, and add libraries and not to repackage original Jar as in Spring Boot solution. Basic manifest builder has been writen, but it needs more work to be suited for OSGi. --- boms-parent/maven-plugin-bom/pom.xml | 91 +++++++ boms-parent/pom.xml | 38 +++ plugs-maven-plugin/pom.xml | 47 +++- .../DefaultExecutionConfiguration.java | 9 + .../maven/generator/PackagePlugMojo.java | 9 +- .../generator/model/ArtifactsLibraries.java | 124 +++++++++ .../model/ExecutionConfiguration.java | 2 + .../maven/generator/model/Libraries.java | 6 - .../generator/model/PlugsMojoException.java | 2 + .../generator/model/StandardLibraries.java | 35 +++ .../generator/packager/ArchiveWriter.java | 84 ++++++ .../maven/generator/packager/Constants.java | 29 ++ .../maven/generator/packager/CrcAndSize.java | 67 +++++ .../generator/packager/EntryTransformer.java | 32 +++ .../maven/generator/packager/EntryWriter.java | 36 +++ .../maven/generator/packager/FileUtils.java | 109 ++++++++ .../packager/IdentityEntryTransformer.java | 34 +++ .../packager/InputStreamEntryWriter.java | 53 ++++ .../maven/generator/packager/JarWriter.java | 255 ++++++++++++++++++ .../packager/LibraryUnpackHandler.java | 45 ++++ .../generator/packager/ManifestBuilder.java | 29 ++ .../packager/ManifestBuilderImpl.java | 48 ++++ .../packager/NeverUnpackHandler.java | 35 +++ .../packager/PlugPackagerFactoryImpl.java | 16 +- .../generator/packager/PlugPackagerImpl.java | 77 +++++- .../generator/packager/UnpackHandler.java | 32 +++ .../generator/packager/WritableLibraries.java | 78 ++++++ .../packager/ZipHeaderPeekInputStream.java | 105 ++++++++ .../src/main/resources/log4j2.xml | 33 +++ pom.xml | 48 +--- 30 files changed, 1540 insertions(+), 68 deletions(-) create mode 100644 boms-parent/maven-plugin-bom/pom.xml create mode 100644 boms-parent/pom.xml create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java create mode 100644 plugs-maven-plugin/src/main/resources/log4j2.xml diff --git a/boms-parent/maven-plugin-bom/pom.xml b/boms-parent/maven-plugin-bom/pom.xml new file mode 100644 index 0000000..c4b0efc --- /dev/null +++ b/boms-parent/maven-plugin-bom/pom.xml @@ -0,0 +1,91 @@ + + + + + 4.0.0 + + pl.wavesoftware.plugs.boms + bom-parent + 0.1.0-SNAPSHOT + + maven-plugin-bom + pom + + Plugs :: BOM :: Maven Plugin BOM + + + 3.3.9 + + + + + + org.apache.maven + maven-plugin-api + ${maven-plugin.version} + provided + + + org.apache.maven + maven-core + ${maven-plugin.version} + provided + + + org.apache.maven + maven-compat + ${maven-plugin.version} + provided + + + org.apache.maven.shared + maven-common-artifact-filters + 3.1.0 + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.3 + provided + + + org.apache.maven + maven-settings-builder + ${maven-plugin.version} + provided + + + org.apache.commons + commons-compress + 1.18 + + + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + 3.3.0 + test + + + + diff --git a/boms-parent/pom.xml b/boms-parent/pom.xml new file mode 100644 index 0000000..7bdf30a --- /dev/null +++ b/boms-parent/pom.xml @@ -0,0 +1,38 @@ + + + + + 4.0.0 + + plugs-parent + pl.wavesoftware.plugs + 0.1.0-SNAPSHOT + + pl.wavesoftware.plugs.boms + bom-parent + pom + + Plugs :: BOM Parent + + + maven-plugin-bom + + diff --git a/plugs-maven-plugin/pom.xml b/plugs-maven-plugin/pom.xml index 35fdf62..8eeb41a 100644 --- a/plugs-maven-plugin/pom.xml +++ b/plugs-maven-plugin/pom.xml @@ -31,7 +31,25 @@ maven-plugin Plugs :: Plugs Maven Plugin + + + + pl.wavesoftware.plugs.boms + maven-plugin-bom + ${project.version} + pom + import + + + + + + pl.wavesoftware.plugs.boms + maven-plugin-bom + ${project.version} + pom + org.apache.maven maven-plugin-api @@ -57,19 +75,40 @@ maven-settings-builder provided + + org.apache.maven.shared + maven-common-artifact-filters + provided + - org.apiguardian - apiguardian-api + pl.wavesoftware + eid-exceptions - org.apache.maven.shared - maven-common-artifact-filters + org.apiguardian + apiguardian-api io.vavr vavr + + org.apache.commons + commons-compress + + + org.slf4j + slf4j-api + + + org.apache.logging.log4j + log4j-slf4j-impl + + + org.apache.logging.log4j + log4j-core + diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java index 72e1049..17b973e 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java @@ -16,6 +16,7 @@ package pl.wavesoftware.plugs.maven.generator; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; @@ -28,6 +29,7 @@ final class DefaultExecutionConfiguration implements ExecutionConfiguration { + private final Log log; private final MavenProject project; private final String classifier; private final boolean attach; @@ -35,12 +37,14 @@ final class DefaultExecutionConfiguration private final String finalName; DefaultExecutionConfiguration( + Log log, MavenProject project, String classifier, boolean attach, File outputDirectory, String finalName ) { + this.log = log; this.project = project; this.classifier = classifier; this.attach = attach; @@ -48,6 +52,11 @@ final class DefaultExecutionConfiguration this.finalName = finalName; } + @Override + public Log getLog() { + return log; + } + @Override public String getFinalName() { return finalName; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java index c1738b9..5f208f0 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java @@ -51,7 +51,7 @@ requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME ) -public final class PackagePlugMojo extends AbstractMojo { +final class PackagePlugMojo extends AbstractMojo { @SuppressWarnings("WeakerAccess") @API(status = Status.STABLE) @@ -184,7 +184,7 @@ private void build() throws PlugsMojoException { if (packager.needsRepackaging()) { buildAndReport(packager, configuration); } else { - getLog().info("Plug package " + finalName + " is up-to-date."); + getLog().info("Plug package " + packager.getTargetFile() + " is up-to-date."); } } @@ -194,10 +194,10 @@ private void buildAndReport( ) throws PlugsMojoException { try { packager.repackageAsPlug(); - getLog().info("Building of " + finalName + " was successful."); + getLog().info("Building of " + packager.getTargetFile() + " was successful."); attachIfNeeded(packager, configuration); } catch (PlugsMojoException ex) { - getLog().error("Building of " + finalName + " has failed."); + getLog().error("Building of " + packager.getTargetFile() + " has failed."); ex.getFailures().ifPresent(failures -> { getLog().error("Reasons of failure:"); for (BuildFailure failure : failures) { @@ -226,6 +226,7 @@ private void attachIfNeeded( private ExecutionConfiguration getConfiguration() { return new DefaultExecutionConfiguration( + getLog(), project, classifier, attach, diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java new file mode 100644 index 0000000..10c996c --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.logging.Log; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * {@link Libraries} backed by Maven {@link Artifact}s. + * + * @author Krzysztof Suszynski + * @author Phillip Webb (Spring Boot project) + * @author Andy Wilkinson (Spring Boot project) + * @author Stephane Nicoll (Spring Boot project) + * @since 0.1.0 + */ +public final class ArtifactsLibraries implements Libraries { + private static final Map SCOPES; + + static { + Map libraryScopes = new HashMap<>(); + libraryScopes.put(Artifact.SCOPE_COMPILE, LibraryScope.COMPILE); + libraryScopes.put(Artifact.SCOPE_RUNTIME, LibraryScope.RUNTIME); + libraryScopes.put(Artifact.SCOPE_PROVIDED, LibraryScope.PROVIDED); + libraryScopes.put(Artifact.SCOPE_SYSTEM, LibraryScope.PROVIDED); + SCOPES = Collections.unmodifiableMap(libraryScopes); + } + + private final Set artifacts; + private final Collection unpacks; + private final Log log; + + public ArtifactsLibraries( + Set artifacts, + Collection unpacks, + Log log + ) { + this.artifacts = Collections.unmodifiableSet(artifacts); + this.unpacks = Collections.unmodifiableCollection(unpacks); + this.log = log; + } + + @Override + public void doWithLibraries(LibraryCallback callback) throws IOException { + Set duplicates = getDuplicates(this.artifacts); + for (Artifact artifact : this.artifacts) { + LibraryScope scope = SCOPES.get(artifact.getScope()); + if (scope != null && artifact.getFile() != null) { + String fileName = getFileName(artifact); + StringBuilder name = new StringBuilder(fileName); + if (duplicates.contains(fileName)) { + this.log.debug("Duplicate found: " + name); + name.delete(0, name.length()); + name.append(artifact.getGroupId()).append("-").append(name); + this.log.debug("Renamed to: " + name); + } + callback.library(new Library( + name.toString(), + artifact.getFile(), + scope, + isUnpackRequired(artifact) + )); + } + } + } + + private boolean isUnpackRequired(Artifact artifact) { + if (this.unpacks != null) { + for (Dependency unpack : this.unpacks) { + if (artifact.getGroupId().equals(unpack.getGroupId()) + && artifact.getArtifactId().equals(unpack.getArtifactId())) { + return true; + } + } + } + return false; + } + + private static Set getDuplicates(Set artifacts) { + Set duplicates = new HashSet<>(); + Set seen = new HashSet<>(); + for (Artifact artifact : artifacts) { + String fileName = getFileName(artifact); + if (artifact.getFile() != null && !seen.add(fileName)) { + duplicates.add(fileName); + } + } + return duplicates; + } + + private static String getFileName(Artifact artifact) { + StringBuilder sb = new StringBuilder(); + sb.append(artifact.getArtifactId()).append("-").append(artifact.getBaseVersion()); + String classifier = artifact.getClassifier(); + if (classifier != null) { + sb.append("-").append(classifier); + } + sb.append(".").append(artifact.getArtifactHandler().getExtension()); + return sb.toString(); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java index 86f38e6..d39df6b 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java @@ -16,6 +16,7 @@ package pl.wavesoftware.plugs.maven.generator.model; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import java.io.File; @@ -25,6 +26,7 @@ * @since 0.1.0 */ public interface ExecutionConfiguration { + Log getLog(); String getFinalName(); String getClassifier(); boolean shouldAttach(); diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java index 84d5309..c0434fa 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java @@ -28,12 +28,6 @@ @FunctionalInterface public interface Libraries { - /** - * Represents no libraries. - */ - Libraries NONE = (callback) -> { - }; - /** * Iterate all relevant libraries. * @param callback a callback for each relevant library. diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java index 6c19813..edc383c 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java @@ -30,6 +30,8 @@ * @since 0.1.0 */ public final class PlugsMojoException extends MojoExecutionException { + private static final long serialVersionUID = 20190115004728L; + @Nullable private final ArrayList failures; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java new file mode 100644 index 0000000..bb0e578 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.model; + +/** + * A class that holds list of standard libraries + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public final class StandardLibraries { + + /** + * Represents no libraries. + */ + Libraries NONE = callback -> {}; + + private StandardLibraries() { + // non reachable + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java new file mode 100644 index 0000000..8e4ae16 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.model.Library; + +import javax.annotation.WillClose; +import java.io.IOException; +import java.io.InputStream; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +/** + * Writer used to write classes into a repackaged JAR. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface ArchiveWriter { + /** + * Write a single entry to the JAR. + * + * @param name the name of the entry + * @param inputStream the input stream content + * @throws IOException if the entry cannot be written + */ + void writeEntry( + String name, + @WillClose InputStream inputStream + ) throws IOException; + + /** + * Write the specified manifest. + * + * @param manifest the manifest to write + * @throws IOException of the manifest cannot be written + */ + void writeManifest(Manifest manifest) throws IOException; + + /** + * Write a nested library. + * + * @param destination the destination of the library + * @param library the library + * @throws IOException if the write fails + */ + void writeNestedLibrary( + String destination, + Library library + ) throws IOException; + + /** + * Write all entries from the specified jar file. + * + * @param jarFile the source jar file + * @throws IOException if the entries cannot be written + */ + void writeEntries(JarFile jarFile) throws IOException; + + void writeEntries( + JarFile jarFile, + UnpackHandler unpackHandler + ) throws IOException; + + void writeEntries( + JarFile jarFile, + EntryTransformer entryTransformer, + UnpackHandler unpackHandler + ) throws IOException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java new file mode 100644 index 0000000..a0d918e --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class Constants { + static final String PLUGS_VERSION_ATTRIBUTE = "Plugs-Version"; + + private Constants() { + // non reachable + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java new file mode 100644 index 0000000..7f608cb --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.CRC32; +import java.util.zip.ZipEntry; + +/** + * Data holder for CRC and Size. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class CrcAndSize { + + private static final int BUFFER_SIZE = 32 * 1024; + private final CRC32 crc = new CRC32(); + + private long size; + + CrcAndSize(File file) throws IOException { + try (FileInputStream inputStream = new FileInputStream(file)) { + load(inputStream); + } + } + + CrcAndSize(InputStream inputStream) throws IOException { + load(inputStream); + } + + void setupStoredEntry(JarArchiveEntry entry) { + entry.setSize(this.size); + entry.setCompressedSize(this.size); + entry.setCrc(this.crc.getValue()); + entry.setMethod(ZipEntry.STORED); + } + + private void load(InputStream inputStream) throws IOException { + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + this.crc.update(buffer, 0, bytesRead); + this.size += bytesRead; + } + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java new file mode 100644 index 0000000..305cb77 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; + +import java.util.jar.JarEntry; + +/** + * An {@code EntryTransformer} enables the transformation of {@link JarEntry jar + * entries} during the writing process. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface EntryTransformer { + JarArchiveEntry transform(JarArchiveEntry jarEntry); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java new file mode 100644 index 0000000..4c89a0e --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Interface used to write jar entry date. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface EntryWriter { + /** + * Write entry data to the specified output stream. + * @param outputStream the destination for the data + * @throws IOException in case of I/O errors + */ + void write(OutputStream outputStream) throws IOException; + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java new file mode 100644 index 0000000..fc21af0 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.WillNotClose; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.security.DigestInputStream; +import java.security.MessageDigest; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; + +/** + * Utilities for manipulating files and directories in Spring Boot tooling. + * + * @author Krzysztof Suszynski + * @author Dave Syer (Spring Boot project) + * @author Phillip Webb (Spring Boot project) + * @since 0.1.0 + */ +final class FileUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class); + private static final int BUFFER_4K = 4098; + private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 }; + + private FileUtils() { + // non reachable + } + + static boolean isZip(File file) { + try { + try (FileInputStream fileInputStream = new FileInputStream(file)) { + return isZip(fileInputStream); + } + } catch (IOException ex) { + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("Can't read file: " + file, ex); + } + return false; + } + } + + private static boolean isZip(InputStream inputStream) throws IOException { + for (byte magicByte : ZIP_FILE_HEADER) { + if (inputStream.read() != magicByte) { + return false; + } + } + return true; + } + + /** + * Generate a SHA.1 Hash for a given file. + * @param file the file to hash + * @return the hash value as a String + * @throws IOException if the file cannot be read + */ + static String sha256Hash(File file) throws IOException { + MessageDigest digest = tryToExecute( + () -> MessageDigest.getInstance("SHA-256"), + "20190115:225947" + ); + try (DigestInputStream inputStream = newDigestInputStream(file, digest)) { + byte[] buffer = new byte[BUFFER_4K]; + //noinspection StatementWithEmptyBody + while (inputStream.read(buffer) != -1) { + // Read the entire stream + } + return bytesToHex(inputStream.getMessageDigest().digest()); + } + } + + @WillNotClose + private static DigestInputStream newDigestInputStream( + File file, + MessageDigest digest + ) throws FileNotFoundException { + return new DigestInputStream(new FileInputStream(file), digest); + } + + private static String bytesToHex(byte[] bytes) { + StringBuilder hex = new StringBuilder(); + for (byte b : bytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java new file mode 100644 index 0000000..c60f6fc --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; + +/** + * An {@code EntryTransformer} that returns the entry unchanged. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class IdentityEntryTransformer implements EntryTransformer { + + @Override + public JarArchiveEntry transform(JarArchiveEntry jarEntry) { + return jarEntry; + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java new file mode 100644 index 0000000..d557b51 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * {@link EntryWriter} that writes content from an {@link InputStream}. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class InputStreamEntryWriter implements EntryWriter { + + private static final int BUFFER_SIZE = 32 * 1024; + private final InputStream inputStream; + private final boolean close; + + InputStreamEntryWriter(InputStream inputStream, boolean close) { + this.inputStream = inputStream; + this.close = close; + } + + @Override + public void write(OutputStream outputStream) throws IOException { + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = this.inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + outputStream.flush(); + if (this.close) { + this.inputStream.close(); + } + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java new file mode 100644 index 0000000..e8e71e5 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java @@ -0,0 +1,255 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; +import org.apache.commons.compress.archivers.zip.UnixStat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.helpers.MessageFormatter; +import pl.wavesoftware.plugs.maven.generator.model.Library; + +import javax.annotation.Nullable; +import javax.annotation.WillClose; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; + +/** + * Writes JAR content, ensuring valid directory entries are always created and + * duplicate items are ignored. + * + * @author Krzysztof Suszynski + * @author Phillip Webb (Spring Boot project) + * @author Andy Wilkinson (Spring Boot project) + * @since 0.1.0 + */ +final class JarWriter implements ArchiveWriter, AutoCloseable { + private static final Logger LOGGER = + LoggerFactory.getLogger(JarWriter.class); + private static final UnpackHandler NEVER_UNPACK = new NeverUnpackHandler(); + + private final JarArchiveOutputStream jarOutput; + private final Set writtenEntries = new HashSet<>(); + + /** + * Create a new {@link JarWriter} instance. + * + * @param file the file to write + * @throws IOException if the file cannot be opened + * @throws FileNotFoundException if the file cannot be found + */ + public JarWriter(File file) throws IOException { + FileOutputStream fileOutputStream = new FileOutputStream(file); + this.jarOutput = new JarArchiveOutputStream(fileOutputStream); + this.jarOutput.setEncoding(StandardCharsets.UTF_8.displayName()); + } + + @Override + public void writeManifest(Manifest manifest) throws IOException { + JarArchiveEntry entry = new JarArchiveEntry("META-INF/MANIFEST.MF"); + writeEntry(entry, manifest::write); + } + + @Override + public void writeEntry( + String entryName, + @WillClose InputStream inputStream + ) throws IOException { + JarArchiveEntry entry = new JarArchiveEntry(entryName); + writeEntry(entry, new InputStreamEntryWriter(inputStream, true)); + } + + @Override + public void writeNestedLibrary( + String destination, + Library library + ) throws IOException { + File file = library.getFile(); + JarArchiveEntry entry = new JarArchiveEntry(destination + library.getName()); + entry.setTime(getNestedLibraryTime(file)); + new CrcAndSize(file).setupStoredEntry(entry); + writeEntry( + entry, + new InputStreamEntryWriter(new FileInputStream(file), true), + new LibraryUnpackHandler(library) + ); + } + + @Override + public void writeEntries(JarFile jarFile) throws IOException { + this.writeEntries(jarFile, new IdentityEntryTransformer(), NEVER_UNPACK); + } + + @Override + public void writeEntries(JarFile jarFile, UnpackHandler unpackHandler) throws IOException { + this.writeEntries(jarFile, new IdentityEntryTransformer(), unpackHandler); + } + + @Override + public void writeEntries( + JarFile jarFile, + EntryTransformer entryTransformer, + UnpackHandler unpackHandler + ) throws IOException { + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarArchiveEntry entry = new JarArchiveEntry(entries.nextElement()); + setUpEntry(jarFile, entry); + try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( + jarFile.getInputStream(entry))) { + EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); + JarArchiveEntry transformedEntry = entryTransformer.transform(entry); + if (transformedEntry != null) { + writeEntry(transformedEntry, entryWriter, unpackHandler); + } + } + } + } + + private void setUpEntry(JarFile jarFile, JarArchiveEntry entry) throws IOException { + try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( + jarFile.getInputStream(entry))) { + if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) { + new CrcAndSize(inputStream).setupStoredEntry(entry); + } else { + entry.setCompressedSize(-1); + } + } + } + + private static long getNestedLibraryTime(File file) { + try { + try (JarFile jarFile = new JarFile(file)) { + Enumeration entries = jarFile.entries(); + Long entry = findTimeOfJarEntries(entries); + if (entry != null) { + return entry; + } + } + } catch (IOException ex) { + // Ignore and just use the source file timestamp + if (LOGGER.isTraceEnabled()) { + LOGGER.trace( + MessageFormatter.format( + "Can't read a supposed JAR file: {}", file + ).toString(), + ex + ); + } + } + return file.lastModified(); + } + + @Nullable + private static Long findTimeOfJarEntries(Enumeration entries) { + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + if (!entry.isDirectory()) { + return entry.getTime(); + } + } + return null; + } + + /** + * Close the writer. + * + * @throws IOException if the file cannot be closed + */ + @Override + public void close() throws IOException { + this.jarOutput.close(); + } + + private void writeEntry( + JarArchiveEntry entry, + EntryWriter entryWriter + ) throws IOException { + writeEntry(entry, entryWriter, NEVER_UNPACK); + } + + /** + * Perform the actual write of a {@link JarEntry}. All other write methods delegate to + * this one. + * + * @param entry the entry to write + * @param entryWriter the entry writer or {@code null} if there is no content + * @param unpackHandler handles possible unpacking for the entry + * @throws IOException in case of I/O errors + */ + private void writeEntry( + JarArchiveEntry entry, + @Nullable EntryWriter entryWriter, + UnpackHandler unpackHandler + ) throws IOException { + String parent = entry.getName(); + if (parent.endsWith("/")) { + parent = parent.substring(0, parent.length() - 1); + entry.setUnixMode(UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM); + } else { + entry.setUnixMode(UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM); + } + if (parent.lastIndexOf('/') != -1) { + parent = parent.substring(0, parent.lastIndexOf('/') + 1); + if (!parent.isEmpty()) { + writeEntry(new JarArchiveEntry(parent), null, unpackHandler); + } + } + + if (this.writtenEntries.add(entry.getName())) { + entryWriter = addUnpackCommentIfNecessary(entry, entryWriter, unpackHandler); + this.jarOutput.putArchiveEntry(entry); + if (entryWriter != null) { + entryWriter.write(this.jarOutput); + } + this.jarOutput.closeArchiveEntry(); + } + } + + private static EntryWriter addUnpackCommentIfNecessary( + JarArchiveEntry entry, + @Nullable EntryWriter entryWriter, + UnpackHandler unpackHandler + ) throws IOException { + if (entryWriter == null || !unpackHandler.requiresUnpack(entry.getName())) { + return entryWriter; + } + ByteArrayOutputStream output = new ByteArrayOutputStream(); + entryWriter.write(output); + entry.setComment("UNPACK:" + unpackHandler.sha256Hash(entry.getName())); + return new InputStreamEntryWriter( + new ByteArrayInputStream(output.toByteArray()), + true + ); + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java new file mode 100644 index 0000000..1d8d7bf --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.model.Library; + +import java.io.IOException; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class LibraryUnpackHandler implements UnpackHandler { + + private final Library library; + + LibraryUnpackHandler(Library library) { + this.library = library; + } + + @Override + public boolean requiresUnpack(String name) { + return this.library.isUnpackRequired(); + } + + @Override + public String sha256Hash(String name) throws IOException { + return FileUtils.sha256Hash(this.library.getFile()); + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java new file mode 100644 index 0000000..2509287 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import java.io.IOException; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface ManifestBuilder { + Manifest buildManifest(JarFile sourceJar) throws IOException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java new file mode 100644 index 0000000..af81612 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import javax.inject.Named; +import java.io.IOException; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import static pl.wavesoftware.plugs.maven.generator.packager.Constants.PLUGS_VERSION_ATTRIBUTE; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class ManifestBuilderImpl implements ManifestBuilder { + @Override + public Manifest buildManifest(JarFile source) throws IOException { + Manifest manifest = source.getManifest(); + if (manifest == null) { + manifest = new Manifest(); + manifest.getMainAttributes() + .putValue("Manifest-Version", "1.0"); + } + manifest = new Manifest(manifest); + String plugsVersion = getClass().getPackage().getImplementationVersion(); + manifest.getMainAttributes().putValue( + PLUGS_VERSION_ATTRIBUTE, + plugsVersion + ); + return manifest; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java new file mode 100644 index 0000000..abefe6a --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class NeverUnpackHandler implements UnpackHandler { + + @Override + public boolean requiresUnpack(String name) { + return false; + } + + @Override + public String sha256Hash(String name) { + throw new UnsupportedOperationException(); + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java index 153608c..f2c71b1 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java @@ -19,6 +19,7 @@ import pl.wavesoftware.plugs.maven.generator.filter.Filter; import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import javax.inject.Inject; import javax.inject.Named; /** @@ -27,8 +28,19 @@ */ @Named final class PlugPackagerFactoryImpl implements PlugPackagerFactory { + + private final ManifestBuilder manifestBuilder; + + @Inject + PlugPackagerFactoryImpl(ManifestBuilder manifestBuilder) { + this.manifestBuilder = manifestBuilder; + } + @Override - public PlugPackager create(ExecutionConfiguration configuration, Filter filter) { - return new PlugPackagerImpl(configuration, filter); + public PlugPackager create( + ExecutionConfiguration configuration, + Filter filter + ) { + return new PlugPackagerImpl(configuration, filter, manifestBuilder); } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java index ed2663f..d6ee81d 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java @@ -19,12 +19,22 @@ import io.vavr.Lazy; import org.apache.maven.artifact.Artifact; import pl.wavesoftware.plugs.maven.generator.filter.Filter; +import pl.wavesoftware.plugs.maven.generator.model.ArtifactsLibraries; import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import pl.wavesoftware.plugs.maven.generator.model.Libraries; import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; import javax.annotation.Nullable; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; import java.util.Set; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; +import static pl.wavesoftware.plugs.maven.generator.packager.Constants.PLUGS_VERSION_ATTRIBUTE; /** * @author Krzysztof Suszynski @@ -34,19 +44,25 @@ final class PlugPackagerImpl implements PlugPackager { private final ExecutionConfiguration configuration; private final Filter filter; + private final ManifestBuilder manifestBuilder; private final Lazy lazyTargetFile; private final Lazy lazySourceArtifact; - PlugPackagerImpl(ExecutionConfiguration configuration, Filter filter) { + PlugPackagerImpl( + ExecutionConfiguration configuration, + Filter filter, + ManifestBuilder manifestBuilder + ) { this.configuration = configuration; this.filter = filter; + this.manifestBuilder = manifestBuilder; lazyTargetFile = Lazy.of(this::calculateTargetFile); lazySourceArtifact = Lazy.of(this::calculateSourceArtifact); } @Override public boolean needsRepackaging() { - return true; + return tryToExecute(() -> !alreadyRepackaged(), "20190116:000119"); } @Override @@ -56,8 +72,17 @@ public void repackageAsPlug() throws PlugsMojoException { Set artifacts = filter.filterDependencies( configuration.getMavenProject().getArtifacts() ); + Libraries libraries = new ArtifactsLibraries( + artifacts, + new ArrayList<>(), + configuration.getLog() + ); - repackage(source, target, artifacts); + try { + repackage(source.getFile(), target, libraries); + } catch (IOException ex) { + throw new PlugsMojoException(ex.getMessage(), ex); + } } @Override @@ -70,12 +95,42 @@ public Artifact getSourceArtifact() { return lazySourceArtifact.get(); } + private boolean alreadyRepackaged() throws IOException { + File targetFile = getTargetFile(); + if (!targetFile.isFile()) { + return false; + } + try (JarFile jarFile = new JarFile(targetFile)) { + Manifest manifest = jarFile.getManifest(); + return manifest != null + && manifest.getMainAttributes() + .getValue(PLUGS_VERSION_ATTRIBUTE) != null; + } + } + private void repackage( - Artifact source, - File target, - Set artifacts - ) { - throw new UnsupportedOperationException("Not yet implemented"); + File source, + File destination, + Libraries libraries + ) throws IOException { + destination = destination.getAbsoluteFile(); + Files.delete(destination.toPath()); + try (JarFile jarFileSource = new JarFile(source)) { + repackage(jarFileSource, destination, libraries); + } + } + + private void repackage( + JarFile sourceJar, + File destination, + Libraries libraries + ) throws IOException { + WritableLibraries writeableLibraries = new WritableLibraries(libraries); + try (JarWriter writer = new JarWriter(destination)) { + writer.writeManifest(manifestBuilder.buildManifest(sourceJar)); + writer.writeEntries(sourceJar, writeableLibraries); + writeableLibraries.write(writer); + } } private Artifact calculateSourceArtifact() { @@ -91,8 +146,10 @@ private File calculateTargetFile() { if (!classifier.isEmpty() && !classifier.startsWith("-")) { classifier = "-" + classifier; } - assert configuration.getOutputDirectory().exists() - || configuration.getOutputDirectory().mkdirs(); + //noinspection RedundantIfStatement + if (!configuration.getOutputDirectory().exists()) { + assert configuration.getOutputDirectory().mkdirs(); + } return new File( configuration.getOutputDirectory(), configuration.getFinalName() diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java new file mode 100644 index 0000000..c2c182e --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import java.io.IOException; + +/** + * An {@code UnpackHandler} determines whether or not unpacking is + * required and provides a SHA256 hash if required. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface UnpackHandler { + boolean requiresUnpack(String name); + + String sha256Hash(String name) throws IOException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java new file mode 100644 index 0000000..2a44882 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.model.Libraries; +import pl.wavesoftware.plugs.maven.generator.model.Library; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +import static pl.wavesoftware.plugs.maven.generator.packager.FileUtils.isZip; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class WritableLibraries implements UnpackHandler { + + private static final String LIBRARY_DESTINATION = "PLUGS-INF/lib/"; + + private final Map libraryEntryNames = new LinkedHashMap<>(); + + WritableLibraries(Libraries libraries) throws IOException { + libraries.doWithLibraries(library -> { + if (isZip(library.getFile())) { + Library existing = this.libraryEntryNames.putIfAbsent( + LIBRARY_DESTINATION + library.getName(), + library + ); + if (existing != null) { + throw new IllegalStateException( + "Duplicate library " + library.getName() + ); + } + } + }); + } + + @Override + public boolean requiresUnpack(String name) { + Library library = this.libraryEntryNames.get(name); + return library != null && library.isUnpackRequired(); + } + + @Override + public String sha256Hash(String name) throws IOException { + Library library = this.libraryEntryNames.get(name); + if (library == null) { + throw new IllegalArgumentException( + "No library found for entry name '" + name + "'"); + } + return FileUtils.sha256Hash(library.getFile()); + } + + void write(JarWriter writer) throws IOException { + for (Map.Entry entry : this.libraryEntryNames.entrySet()) { + writer.writeNestedLibrary( + entry.getKey().substring(0, entry.getKey().lastIndexOf('/') + 1), + entry.getValue()); + } + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java new file mode 100644 index 0000000..ac82195 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import java.io.ByteArrayInputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +/** + * {@link InputStream} that can peek ahead at zip header bytes. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class ZipHeaderPeekInputStream extends FilterInputStream { + + private static final byte[] ZIP_HEADER = new byte[]{0x50, 0x4b, 0x03, 0x04}; + + private final byte[] header; + private final int headerLength; + + private int position; + private ByteArrayInputStream headerStream; + + ZipHeaderPeekInputStream(InputStream in) throws IOException { + super(in); + this.header = new byte[4]; + this.headerLength = in.read(this.header); + this.headerStream = new ByteArrayInputStream( + this.header, + 0, + this.headerLength + ); + } + + @Override + public int read() throws IOException { + int read = (this.headerStream != null) + ? this.headerStream.read() + : -1; + if (read != -1) { + this.position++; + if (this.position >= this.headerLength) { + this.headerStream = null; + } + return read; + } + return super.read(); + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + int read = (this.headerStream != null) + ? this.headerStream.read(b, off, len) + : -1; + if (read <= 0) { + return readRemainder(b, off, len); + } + this.position += read; + if (read < len) { + int remainderRead = readRemainder(b, off + read, len - read); + if (remainderRead > 0) { + read += remainderRead; + } + } + if (this.position >= this.headerLength) { + this.headerStream = null; + } + return read; + } + + boolean hasZipHeader() { + return Arrays.equals(this.header, ZIP_HEADER); + } + + private int readRemainder(byte[] b, int off, int len) throws IOException { + int read = super.read(b, off, len); + if (read > 0) { + this.position += read; + } + return read; + } + +} diff --git a/plugs-maven-plugin/src/main/resources/log4j2.xml b/plugs-maven-plugin/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2302b4e --- /dev/null +++ b/plugs-maven-plugin/src/main/resources/log4j2.xml @@ -0,0 +1,33 @@ + + + + + %hightlight{%5p} %-40.40c{1.} %m%n%ex + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 78916b1..97e2757 100644 --- a/pom.xml +++ b/pom.xml @@ -54,7 +54,7 @@ - 3.0 + 3.3 @@ -63,6 +63,7 @@ plugs-felix plugs-spring plugs-maven-plugin + boms-parent examples testing @@ -161,41 +162,6 @@ eid-exceptions 2.0.0 - - org.apache.maven - maven-plugin-api - ${maven.version} - provided - - - org.apache.maven - maven-core - ${maven.version} - provided - - - org.apache.maven - maven-compat - ${maven.version} - provided - - - org.apache.maven.shared - maven-common-artifact-filters - 3.1.0 - - - org.apache.maven.plugin-tools - maven-plugin-annotations - ${maven.version} - provided - - - org.apache.maven - maven-settings-builder - ${maven.version} - provided - @@ -210,12 +176,6 @@ 1.1.3 test - - org.apache.maven.plugin-testing - maven-plugin-testing-harness - 3.3.0 - test - @@ -363,6 +323,10 @@ maven-site-plugin 3.7.1 + + maven-plugin-plugin + 3.6.0 + org.codehaus.gmaven groovy-maven-plugin From 4d61e8c5c39a6466baf64cd1dea1a172d82b1f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Thu, 17 Jan 2019 01:02:12 +0100 Subject: [PATCH 05/23] Main flow of Maven plugin There are couple of further tasks: * Better decision of noop is needed, based on source hash. * Write source artifact compiled sources into jar * Create real imports, from provided scope * Define dependencies, imports, sources, name & version in OSGi bundle manifest --- .../maven/generator/PackagePlugMojo.java | 23 ++++----- .../generator/packager/ArchiveWriter.java | 5 ++ .../packager/ArchiveWriterEvent.java | 26 ++++++++++ ...actory.java => ArchiveWriterListener.java} | 15 ++++-- .../maven/generator/packager/Constants.java | 1 + .../maven/generator/packager/JarWriter.java | 26 +++++++++- .../packager/LibrariesCollector.java | 38 +++++++++++++++ .../packager/LibraryHasBeenWritten.java | 35 ++++++++++++++ .../generator/packager/ManifestBuilder.java | 23 +++++++-- .../packager/ManifestBuilderImpl.java | 41 ++++++++++------ .../{PlugPackager.java => Packager.java} | 16 +++++-- .../generator/packager/PackagerFactory.java | 39 +++++++++++++++ ...toryImpl.java => PackagerFactoryImpl.java} | 8 ++-- ...lugPackagerImpl.java => PackagerImpl.java} | 47 ++++++++++++------- .../generator/packager/WritableLibraries.java | 3 +- .../maven/generator/PackagePlugMojoTest.java | 33 +++++++++++-- .../log4j2/HighlightColorConverter.java | 2 +- testing/src/main/resources/log4j2-test.xml | 2 +- .../log4j2/HighlightColorConverterTest.java | 12 ++--- 19 files changed, 321 insertions(+), 74 deletions(-) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/{PlugPackagerFactory.java => ArchiveWriterListener.java} (75%) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/{PlugPackager.java => Packager.java} (76%) create mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/{PlugPackagerFactoryImpl.java => PackagerFactoryImpl.java} (83%) rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/{PlugPackagerImpl.java => PackagerImpl.java} (81%) diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java index 5f208f0..30c64b3 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java @@ -17,6 +17,7 @@ package pl.wavesoftware.plugs.maven.generator; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -32,8 +33,8 @@ import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; import pl.wavesoftware.plugs.maven.generator.model.Include; import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; -import pl.wavesoftware.plugs.maven.generator.packager.PlugPackager; -import pl.wavesoftware.plugs.maven.generator.packager.PlugPackagerFactory; +import pl.wavesoftware.plugs.maven.generator.packager.Packager; +import pl.wavesoftware.plugs.maven.generator.packager.PackagerFactory; import javax.inject.Inject; import java.io.File; @@ -60,7 +61,7 @@ final class PackagePlugMojo extends AbstractMojo { /** * The package factory */ - private final PlugPackagerFactory packagerFactory; + private final PackagerFactory packagerFactory; /** * The filter factory @@ -155,7 +156,7 @@ final class PackagePlugMojo extends AbstractMojo { @Inject PackagePlugMojo( - PlugPackagerFactory packagerFactory, + PackagerFactory packagerFactory, FilterFactory filterFactory, MavenProjectHelper projectHelper ) { @@ -165,7 +166,7 @@ final class PackagePlugMojo extends AbstractMojo { } @Override - public void execute() throws PlugsMojoException { + public void execute() throws MojoExecutionException { if ("pom".equals(project.getPackaging())) { getLog().debug(GOAL + " goal could not be applied to pom project."); return; @@ -177,10 +178,10 @@ public void execute() throws PlugsMojoException { build(); } - private void build() throws PlugsMojoException { + private void build() throws MojoExecutionException { Filter filter = filterFactory.create(includes, excludes); ExecutionConfiguration configuration = getConfiguration(); - PlugPackager packager = packagerFactory.create(configuration, filter); + Packager packager = packagerFactory.create(configuration, filter); if (packager.needsRepackaging()) { buildAndReport(packager, configuration); } else { @@ -189,11 +190,11 @@ private void build() throws PlugsMojoException { } private void buildAndReport( - PlugPackager packager, + Packager packager, ExecutionConfiguration configuration - ) throws PlugsMojoException { + ) throws MojoExecutionException { try { - packager.repackageAsPlug(); + packager.repackage(); getLog().info("Building of " + packager.getTargetFile() + " was successful."); attachIfNeeded(packager, configuration); } catch (PlugsMojoException ex) { @@ -209,7 +210,7 @@ private void buildAndReport( } private void attachIfNeeded( - PlugPackager packager, + Packager packager, ExecutionConfiguration configuration ) { if (configuration.shouldAttach()) { diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java index 8e4ae16..6265ba8 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java @@ -81,4 +81,9 @@ void writeEntries( EntryTransformer entryTransformer, UnpackHandler unpackHandler ) throws IOException; + + void addListener( + Class eventType, + ArchiveWriterListener listener + ); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java new file mode 100644 index 0000000..8e6eff2 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +/** + * An event + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface ArchiveWriterEvent { +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterListener.java similarity index 75% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java rename to plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterListener.java index 6fef091..df0f240 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactory.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterListener.java @@ -16,13 +16,18 @@ package pl.wavesoftware.plugs.maven.generator.packager; -import pl.wavesoftware.plugs.maven.generator.filter.Filter; -import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; - /** + * A listener for specific archive writer event + * * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface PlugPackagerFactory { - PlugPackager create(ExecutionConfiguration configuration, Filter filter); +interface ArchiveWriterListener { + + /** + * Handles an raised event + * + * @param event an event that was raised + */ + void handle(E event); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java index a0d918e..e8dcc61 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java @@ -22,6 +22,7 @@ */ final class Constants { static final String PLUGS_VERSION_ATTRIBUTE = "Plugs-Version"; + static final String LIBRARY_DESTINATION = "PLUGS-INF/lib/"; private Constants() { // non reachable diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java index e8e71e5..bf04a05 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java @@ -16,6 +16,8 @@ package pl.wavesoftware.plugs.maven.generator.packager; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; import org.apache.commons.compress.archivers.zip.UnixStat; @@ -59,6 +61,10 @@ final class JarWriter implements ArchiveWriter, AutoCloseable { private final JarArchiveOutputStream jarOutput; private final Set writtenEntries = new HashSet<>(); + private final Multimap< + Class, + ArchiveWriterListener + > listeners = HashMultimap.create(); /** * Create a new {@link JarWriter} instance. @@ -67,7 +73,7 @@ final class JarWriter implements ArchiveWriter, AutoCloseable { * @throws IOException if the file cannot be opened * @throws FileNotFoundException if the file cannot be found */ - public JarWriter(File file) throws IOException { + JarWriter(File file) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(file); this.jarOutput = new JarArchiveOutputStream(fileOutputStream); this.jarOutput.setEncoding(StandardCharsets.UTF_8.displayName()); @@ -102,6 +108,13 @@ public void writeNestedLibrary( new InputStreamEntryWriter(new FileInputStream(file), true), new LibraryUnpackHandler(library) ); + LibraryHasBeenWritten event = new LibraryHasBeenWritten(library); + for (ArchiveWriterListener listener : listeners.get(LibraryHasBeenWritten.class)) { + @SuppressWarnings("unchecked") + ArchiveWriterListener libraryListener = + (ArchiveWriterListener) listener; + libraryListener.handle(event); + } } @Override @@ -135,7 +148,16 @@ public void writeEntries( } } - private void setUpEntry(JarFile jarFile, JarArchiveEntry entry) throws IOException { + @Override + public void addListener( + Class eventType, + ArchiveWriterListener listener + ) { + listeners.put(eventType, listener); + } + + private static void setUpEntry(JarFile jarFile, JarArchiveEntry entry) + throws IOException { try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( jarFile.getInputStream(entry))) { if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) { diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java new file mode 100644 index 0000000..bc5a259 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import pl.wavesoftware.plugs.maven.generator.model.Library; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class LibrariesCollector { + + private final java.util.Set libraries = new java.util.HashSet<>(); + + void collect(Library library) { + libraries.add(library); + } + + public Set getCollected() { + return HashSet.ofAll(libraries); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java new file mode 100644 index 0000000..cccb495 --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.model.Library; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class LibraryHasBeenWritten implements ArchiveWriterEvent { + private final Library library; + + LibraryHasBeenWritten(Library library) { + this.library = library; + } + + public Library getLibrary() { + return library; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java index 2509287..1aa56a6 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java @@ -16,14 +16,31 @@ package pl.wavesoftware.plugs.maven.generator.packager; -import java.io.IOException; -import java.util.jar.JarFile; +import io.vavr.collection.Set; +import org.apache.maven.artifact.Artifact; +import pl.wavesoftware.plugs.maven.generator.model.Library; + import java.util.jar.Manifest; /** + * A builder that builds a OSGi manifest for a Plug + * * @author Krzysztof Suszynski * @since 0.1.0 */ interface ManifestBuilder { - Manifest buildManifest(JarFile sourceJar) throws IOException; + + /** + * Builds a OSGi manifest of a Jar based on artifact information + * + * @param artifact an artifact + * @param dependencies dependencies of an artifact that ware embedded in Jar + * @param imports a imports to be declared + * @return a OSGi manifest for a Plug + */ + Manifest buildManifest( + Artifact artifact, + Set dependencies, + Set imports + ); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java index af81612..0222880 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java @@ -16,9 +16,14 @@ package pl.wavesoftware.plugs.maven.generator.packager; +import io.vavr.collection.Set; +import org.apache.maven.artifact.Artifact; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.wavesoftware.plugs.maven.generator.model.Library; + import javax.inject.Named; -import java.io.IOException; -import java.util.jar.JarFile; +import java.util.jar.Attributes; import java.util.jar.Manifest; import static pl.wavesoftware.plugs.maven.generator.packager.Constants.PLUGS_VERSION_ATTRIBUTE; @@ -29,20 +34,28 @@ */ @Named final class ManifestBuilderImpl implements ManifestBuilder { + + private static final Logger LOGGER = + LoggerFactory.getLogger(ManifestBuilderImpl.class); + @Override - public Manifest buildManifest(JarFile source) throws IOException { - Manifest manifest = source.getManifest(); - if (manifest == null) { - manifest = new Manifest(); - manifest.getMainAttributes() - .putValue("Manifest-Version", "1.0"); - } - manifest = new Manifest(manifest); + public Manifest buildManifest( + Artifact artifact, + Set dependencies, + Set imports + ) { + Manifest manifest = new Manifest(); + Attributes attributes = manifest.getMainAttributes(); String plugsVersion = getClass().getPackage().getImplementationVersion(); - manifest.getMainAttributes().putValue( - PLUGS_VERSION_ATTRIBUTE, - plugsVersion - ); + + attributes.putValue("Manifest-Version", "1.0"); + attributes.putValue(PLUGS_VERSION_ATTRIBUTE, plugsVersion); + + LOGGER.warn("Define dependencies"); + LOGGER.warn("Define imports"); + LOGGER.warn("Define sources"); + LOGGER.warn("Define name & version"); + return manifest; } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Packager.java similarity index 76% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java rename to plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Packager.java index 3512383..86cb337 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackager.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Packager.java @@ -22,13 +22,21 @@ import java.io.File; /** - * A Plugs packager that repackage jar as a plug module. All dependencies will - * be embedded as a internal dependencies. + *

+ * A Plugs packager that package jar as a plug module. + * + *

+ * Code will be embedded in standard jar location. All dependencies will be + * embedded as a internal dependencies. Dependencies in provided scope will + * become imports in bundle manifest. + *

+ * All that above means that Plug module is usable as a standard Jar, but + * have extra parts that makes it usable as a OSGi bundle. * * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface PlugPackager { +public interface Packager { /** * Checks where repackage action is required * @@ -41,7 +49,7 @@ public interface PlugPackager { * * @throws PlugsMojoException if something goes wrong in repackage process */ - void repackageAsPlug() throws PlugsMojoException; + void repackage() throws PlugsMojoException; /** * Gets a source artifact that will be a base for re-packaging as a Plug diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java new file mode 100644 index 0000000..3a6c69c --- /dev/null +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.maven.generator.packager; + +import pl.wavesoftware.plugs.maven.generator.filter.Filter; +import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; + +/** + * A packager factory to produce instances of {@link Packager}. + * + * @author Krzysztof Suszynski + * @see Packager + * @since 0.1.0 + */ +public interface PackagerFactory { + + /** + * Creates a packages from execution configuration and filter of dependencies. + * + * @param configuration a configuration of execution + * @param filter a filter for dependencies + * @return a created packager instance + */ + Packager create(ExecutionConfiguration configuration, Filter filter); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java similarity index 83% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java rename to plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java index f2c71b1..6513f61 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerFactoryImpl.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java @@ -27,20 +27,20 @@ * @since 0.1.0 */ @Named -final class PlugPackagerFactoryImpl implements PlugPackagerFactory { +final class PackagerFactoryImpl implements PackagerFactory { private final ManifestBuilder manifestBuilder; @Inject - PlugPackagerFactoryImpl(ManifestBuilder manifestBuilder) { + PackagerFactoryImpl(ManifestBuilder manifestBuilder) { this.manifestBuilder = manifestBuilder; } @Override - public PlugPackager create( + public Packager create( ExecutionConfiguration configuration, Filter filter ) { - return new PlugPackagerImpl(configuration, filter, manifestBuilder); + return new PackagerImpl(configuration, filter, manifestBuilder); } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java similarity index 81% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java rename to plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java index d6ee81d..98d3050 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PlugPackagerImpl.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java @@ -17,11 +17,15 @@ package pl.wavesoftware.plugs.maven.generator.packager; import io.vavr.Lazy; +import io.vavr.collection.HashSet; import org.apache.maven.artifact.Artifact; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import pl.wavesoftware.plugs.maven.generator.filter.Filter; import pl.wavesoftware.plugs.maven.generator.model.ArtifactsLibraries; import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; import pl.wavesoftware.plugs.maven.generator.model.Libraries; +import pl.wavesoftware.plugs.maven.generator.model.Library; import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; import javax.annotation.Nullable; @@ -40,7 +44,10 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -final class PlugPackagerImpl implements PlugPackager { +final class PackagerImpl implements Packager { + + private static final Logger LOGGER = + LoggerFactory.getLogger(PackagerImpl.class); private final ExecutionConfiguration configuration; private final Filter filter; @@ -48,7 +55,7 @@ final class PlugPackagerImpl implements PlugPackager { private final Lazy lazyTargetFile; private final Lazy lazySourceArtifact; - PlugPackagerImpl( + PackagerImpl( ExecutionConfiguration configuration, Filter filter, ManifestBuilder manifestBuilder @@ -66,7 +73,7 @@ public boolean needsRepackaging() { } @Override - public void repackageAsPlug() throws PlugsMojoException { + public void repackage() throws PlugsMojoException { Artifact source = getSourceArtifact(); File target = getTargetFile(); Set artifacts = filter.filterDependencies( @@ -79,7 +86,7 @@ public void repackageAsPlug() throws PlugsMojoException { ); try { - repackage(source.getFile(), target, libraries); + repackage(source, target, libraries); } catch (IOException ex) { throw new PlugsMojoException(ex.getMessage(), ex); } @@ -102,6 +109,7 @@ private boolean alreadyRepackaged() throws IOException { } try (JarFile jarFile = new JarFile(targetFile)) { Manifest manifest = jarFile.getManifest(); + LOGGER.warn("Better decision of noop is needed, based on source hash."); return manifest != null && manifest.getMainAttributes() .getValue(PLUGS_VERSION_ATTRIBUTE) != null; @@ -109,27 +117,30 @@ private boolean alreadyRepackaged() throws IOException { } private void repackage( - File source, + Artifact source, File destination, Libraries libraries ) throws IOException { destination = destination.getAbsoluteFile(); - Files.delete(destination.toPath()); - try (JarFile jarFileSource = new JarFile(source)) { - repackage(jarFileSource, destination, libraries); - } - } - - private void repackage( - JarFile sourceJar, - File destination, - Libraries libraries - ) throws IOException { + Files.deleteIfExists(destination.toPath()); WritableLibraries writeableLibraries = new WritableLibraries(libraries); + LibrariesCollector collector = new LibrariesCollector(); try (JarWriter writer = new JarWriter(destination)) { - writer.writeManifest(manifestBuilder.buildManifest(sourceJar)); - writer.writeEntries(sourceJar, writeableLibraries); + writer.addListener( + LibraryHasBeenWritten.class, + event -> collector.collect(event.getLibrary()) + ); writeableLibraries.write(writer); + LOGGER.warn("Write source artifact compiled sources"); + + LOGGER.warn("Create real imports, from provided scope"); + HashSet imports = HashSet.empty(); + Manifest manifest = manifestBuilder.buildManifest( + source, + collector.getCollected(), + imports + ); + writer.writeManifest(manifest); } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java index 2a44882..b36c5ca 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java +++ b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java @@ -23,6 +23,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import static pl.wavesoftware.plugs.maven.generator.packager.Constants.LIBRARY_DESTINATION; import static pl.wavesoftware.plugs.maven.generator.packager.FileUtils.isZip; /** @@ -31,8 +32,6 @@ */ final class WritableLibraries implements UnpackHandler { - private static final String LIBRARY_DESTINATION = "PLUGS-INF/lib/"; - private final Map libraryEntryNames = new LinkedHashMap<>(); WritableLibraries(Libraries libraries) throws IOException { diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java index f538abc..cef9079 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java +++ b/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java @@ -18,6 +18,7 @@ import org.apache.maven.plugin.logging.Log; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -26,12 +27,19 @@ import pl.wavesoftware.maven.junit5.MojoBuilderFactory; import pl.wavesoftware.maven.junit5.MojoRuleExtension; +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Comparator; import static org.assertj.core.api.Assertions.assertThatCode; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.contains; import static org.mockito.Mockito.verify; +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; /** * @author Krzysztof Suszynski @@ -43,9 +51,28 @@ }) class PackagePlugMojoTest { + private final Path pomDirectory = Paths.get("simpliest"); + @Mock private Log log; + @BeforeEach + void before() throws URISyntaxException, IOException { + Path testClasses = + new File(checkNotNull( + getClass().getClassLoader().getResource("."), + "20190117:004601" + ).toURI()).toPath(); + Path target = testClasses.resolve(pomDirectory).resolve("target"); + if (target.toFile().exists()) { + Files.walk(target) + .sorted(Comparator.reverseOrder()) + .forEach(path -> tryToExecute(() -> + Files.deleteIfExists(path), "20190117:005048") + ); + } + } + @AfterEach void after() { Mockito.verifyNoMoreInteractions(log); @@ -55,7 +82,6 @@ void after() { @Test void execute(MojoBuilderFactory factory) { // given - Path pomDirectory = Paths.get("simpliest"); PackagePlugMojo mojo = factory .builder(PackagePlugMojo.class) .withPomDirectory(pomDirectory) @@ -64,6 +90,7 @@ void execute(MojoBuilderFactory factory) { // when & then assertThatCode(mojo::execute).doesNotThrowAnyException(); - verify(log).debug(eq("Packager is: default, project: simpliest-0.1.0")); + verify(log).info(contains("simpliest/target/simpliest-0.1.0-plug.jar was successful.")); + verify(log).info(contains("Artifact attached to the build")); } } diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java index 2be5913..e4def53 100644 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java +++ b/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java @@ -67,7 +67,7 @@ public final class HighlightColorConverter extends LogEventPatternConverter { static { Map ansiElements = new HashMap<>(); - ansiElements.put("faint", AnsiColor.WHITE); + ansiElements.put("faint", AnsiComposite.of(AnsiColor.WHITE, AnsiStyle.FAINT)); ansiElements.put("black", AnsiColor.BLACK); ansiElements.put("red", AnsiColor.RED); ansiElements.put("green", AnsiColor.GREEN); diff --git a/testing/src/main/resources/log4j2-test.xml b/testing/src/main/resources/log4j2-test.xml index 022dc4f..154a4b9 100644 --- a/testing/src/main/resources/log4j2-test.xml +++ b/testing/src/main/resources/log4j2-test.xml @@ -18,7 +18,7 @@ strict="true"> %hl{%5p} %hl{%d{HH:mm:ss.SSS}}{faint} %hl{[%10.10t]}{yellow} %hl{%-40.40c{1.}}{cyan} %hl{:}{faint} %m%n%ex + name="CONSOLE_LOG_PATTERN">%hl{%5p} %hl{%d{HH:mm:ss.SSS}}{faint} %hl{[%10.10t]}{yellow} %hl{%-40.40c{1.}}{cyan} %hl{:}{faint} %m%n%hl{%ex} diff --git a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java index b7d58e5..4e2a924 100644 --- a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java +++ b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java @@ -71,12 +71,12 @@ void format() { .hasSize(EXPECTED_SIZE) .extracting(CollectorManager.CollectedEvent::getFormattedMessage) .containsExactly( - "\u001B[32m INFO\u001B[0;39m \u001B[33m[ main]" + - "\u001B[0;39m \u001B[36mp.w.p.t.l.HighlightColorConverterTest" + - " \u001B[0;39m \u001B[37m:\u001B[0;39m info\n", - "\u001B[31;1mERROR\u001B[0;39m \u001B[33m[ main]" + - "\u001B[0;39m \u001B[36mp.w.p.t.l.HighlightColorConverterTest" + - " \u001B[0;39m \u001B[37m:\u001B[0;39m error\n" + "\u001B[32m INFO\u001B[0;39m \u001B[33m[ main]\u001B[0;39m " + + "\u001B[36mp.w.p.t.l.HighlightColorConverterTest " + + "\u001B[0;39m \u001B[37;2m:\u001B[0;39m info\n", + "\u001B[31;1mERROR\u001B[0;39m \u001B[33m[ main]\u001B[0;39m " + + "\u001B[36mp.w.p.t.l.HighlightColorConverterTest " + + "\u001B[0;39m \u001B[37;2m:\u001B[0;39m error\n" ); } } From ab27022e53f8c5b688f2d8c7753558083159cf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Fri, 1 Feb 2019 01:01:43 +0100 Subject: [PATCH 06/23] Rewrite to repackage main jar artifact Rewiting plugin to repackage main jar artifact as a plug. The behiviour is similar to Spring Boot plugin, as source artifact can hold any number of user customisations like custom files and manifests. Reusing it make whole process dependant on artifact, which is a minor downside, but offers better coverage of all users needs. To be able to easily test and also to enable to write other plugins in future, like Gradle, I need to move code that is not directly linked to maven to separate package: core. * Maven backed SLF4J Logger * Maven backed coordinates * Almost all of configuration, except Project class impl * Maven libraries scaffold * All Maven backend factories should be completed * Switched to VAVR on Core nad Maven tools * Cross platform fixes --- boms-parent/maven-plugin-bom/pom.xml | 7 + .../plugs/core/AbstractFrameworkProducer.java | 2 +- .../plugs/core/PlugsOsgiContainerTest.java | 2 +- .../generator/model/PlugsMojoException.java | 51 ---- .../packager/ManifestBuilderImpl.java | 61 ----- .../generator/packager/PackagerImpl.java | 186 -------------- .../src/main/resources/log4j2.xml | 33 --- pom.xml | 34 ++- testing/src/main/resources/log4j2-test.xml | 9 +- .../log4j2/HighlightColorConverterTest.java | 7 +- .../plugs-maven-plugin}/pom.xml | 25 +- .../tools/maven/plugin}/PackagePlugMojo.java | 127 +++++----- .../tools/maven/plugin/PackagerFactories.java | 33 +++ .../maven/plugin/PackagerFactoriesImpl.java | 71 ++++++ .../filter/AbstractDependencyFilter.java | 12 +- .../maven/plugin}/filter/ExcludeFilter.java | 6 +- .../maven/plugin}/filter/FilterFactory.java | 13 +- .../maven/plugin}/filter/IncludeFilter.java | 6 +- .../maven/plugin/filter/MavenFilter.java | 33 ++- .../plugin/filter/MavenFilterFactory.java | 21 +- .../maven/plugin/filter/package-info.java | 24 ++ .../tools/maven/plugin/io/LoggerFactory.java | 38 +++ .../tools/maven/plugin/io/MavenLogger.java | 230 ++++++++++++++++++ .../maven/plugin/io/MavenLoggerFactory.java | 20 +- .../tools/maven/plugin/io}/package-info.java | 2 +- .../maven/plugin/mapper/ArtifactMapper.java | 14 +- .../plugin/mapper/ArtifactMapperImpl.java | 43 ++++ .../maven/plugin/mapper/MavenArtifact.java | 59 +++++ .../plugin/mapper/MavenBackedProject.java | 106 ++++++++ .../plugin/mapper/MavenLibrariesFactory.java | 51 ++++ .../mapper/MavenPackagerConfiguration.java | 64 +++-- .../MavenPackagerConfigurationFactory.java | 60 +++++ .../mapper/MavenPackagerCoordinates.java | 75 ++++++ .../mapper/PackagerConfigurationFactory.java | 51 ++++ .../maven/plugin/mapper/package-info.java | 24 ++ .../model/AbstractFilterableDependency.java | 33 ++- .../tools/maven/plugin}/model/Exclude.java | 2 +- .../tools/maven/plugin}/model/Include.java | 2 +- .../maven/plugin/model/MavenLibraries.java | 72 +++--- .../maven/plugin/model}/package-info.java | 2 +- .../tools/maven/plugin}/package-info.java | 2 +- .../junit5/DefaultMojoConfigurator.java | 15 +- .../maven/testing/junit5/MavenInvoker.java | 32 +++ .../testing/junit5/MavenInvokerExtension.java | 60 +++++ .../junit5/MavenProjectCustomizer.java | 8 +- .../maven/testing}/junit5/MojoBuilder.java | 2 +- .../testing}/junit5/MojoBuilderImpl.java | 2 +- .../testing}/junit5/MojoConfigurator.java | 2 +- .../maven/testing/junit5/MojoExtension.java | 14 +- .../maven/testing/junit5/MojoFactory.java | 7 +- .../maven/testing/junit5/MojoFactoryImpl.java | 23 +- .../maven/testing}/junit5/package-info.java | 2 +- .../tools/maven/plugin/PackagePlugMojoIT.java | 78 +++--- .../src/test/resources/simpliest/pom.xml | 4 + tools/plugs-packager-core/pom.xml | 60 +++++ .../plugs/tools/packager/core/Constants.java | 33 +++ .../tools/packager/core/DefaultPackager.java | 193 +++++++++++++++ .../packager/core/DefaultPackagerFactory.java | 66 +++++ .../core/DefaultRepackagingIsRequired.java | 69 ++++++ .../DefaultRepackagingIsRequiredFactory.java | 24 +- .../plugs/tools/packager/core}/Packager.java | 36 +-- .../tools/packager/core}/PackagerFactory.java | 8 +- .../core/RepackagingIsRequiredFactory.java | 36 +++ .../packager/core/jar}/ArchiveWriter.java | 4 +- .../core/jar}/ArchiveWriterEvent.java | 4 +- .../core/jar}/ArchiveWriterListener.java | 4 +- .../tools/packager/core/jar}/CrcAndSize.java | 2 +- .../packager/core/jar}/EntryTransformer.java | 2 +- .../tools/packager/core/jar}/EntryWriter.java | 2 +- .../tools/packager/core/jar/FileDigest.java | 37 +++ .../packager/core/jar/FileDigestImpl.java | 43 ++++ .../tools/packager/core/jar}/FileUtils.java | 2 +- .../core/jar}/IdentityEntryTransformer.java | 2 +- .../core/jar}/InputStreamEntryWriter.java | 2 +- .../tools/packager/core/jar}/JarWriter.java | 29 ++- .../core/jar}/LibrariesCollector.java | 10 +- .../core/jar}/LibraryHasBeenWritten.java | 6 +- .../core/jar}/LibraryUnpackHandler.java | 4 +- .../core/jar}/NeverUnpackHandler.java | 4 +- .../packager/core/jar}/UnpackHandler.java | 2 +- .../packager/core/jar}/WritableLibraries.java | 21 +- .../core/jar}/ZipHeaderPeekInputStream.java | 2 +- .../tools/packager/core/jar/package-info.java | 24 ++ .../core/manifest}/ManifestBuilder.java | 22 +- .../core/manifest/ManifestBuilderImpl.java | 86 +++++++ .../packager/core/manifest/package-info.java | 24 ++ .../tools/packager/core/model/Artifact.java | 57 +++++ .../packager/core/model/ArtifactType.java | 51 ++++ .../tools/packager/core/model/CodeBlock.java | 29 +++ .../tools/packager/core/model}/Filter.java | 16 +- .../core}/model/FilterableDependency.java | 11 +- .../packager/core/model/IoPossibleBlock.java | 9 +- .../core/model/IoPossibleSupplier.java | 35 +++ .../tools/packager/core}/model/Libraries.java | 2 +- .../tools/packager/core}/model/Library.java | 10 +- .../packager/core}/model/LibraryCallback.java | 2 +- .../packager/core}/model/LibraryScope.java | 10 +- .../plugs/tools/packager/core/model/Or.java | 32 +++ .../core/model/PackagerConfiguration.java | 55 +++++ .../core/model/PackagerCoordinates.java | 41 ++++ .../tools/packager/core/model/Project.java | 72 ++++++ .../packager/core/model/RepackageFailed.java | 96 ++++++++ .../core/model/RepackagingIsRequired.java | 32 +++ .../core}/model/StandardLibraries.java | 2 +- .../packager/core/model/package-info.java | 24 ++ .../tools/packager/core}/package-info.java | 2 +- .../packager/core/spi/LibrariesFactory.java | 40 +++ .../tools/packager/core/spi/package-info.java | 24 ++ .../META-INF/sisu/javax.inject.Named | 4 + tools/pom.xml | 39 +++ 110 files changed, 2787 insertions(+), 733 deletions(-) delete mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java delete mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java delete mode 100644 plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java delete mode 100644 plugs-maven-plugin/src/main/resources/log4j2.xml rename {plugs-maven-plugin => tools/plugs-maven-plugin}/pom.xml (85%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/PackagePlugMojo.java (62%) create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/filter/AbstractDependencyFilter.java (88%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/filter/ExcludeFilter.java (86%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/filter/FilterFactory.java (72%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/filter/IncludeFilter.java (87%) rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java (53%) rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java (70%) create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/LoggerFactory.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLogger.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerFactory.java (70%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io}/package-info.java (93%) rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java (71%) create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java (50%) create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/model/AbstractFilterableDependency.java (72%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/model/Exclude.java (93%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/model/Include.java (93%) rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java (62%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model}/package-info.java (93%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin}/package-info.java (94%) rename {plugs-maven-plugin/src/test/java/pl/wavesoftware/maven => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing}/junit5/DefaultMojoConfigurator.java (76%) create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvoker.java create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvokerExtension.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenProjectCustomizer.java (80%) rename {plugs-maven-plugin/src/test/java/pl/wavesoftware/maven => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing}/junit5/MojoBuilder.java (96%) rename {plugs-maven-plugin/src/test/java/pl/wavesoftware/maven => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing}/junit5/MojoBuilderImpl.java (98%) rename {plugs-maven-plugin/src/test/java/pl/wavesoftware/maven => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing}/junit5/MojoConfigurator.java (96%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoExtension.java (86%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java (80%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java (69%) rename {plugs-maven-plugin/src/test/java/pl/wavesoftware/maven => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing}/junit5/package-info.java (94%) rename plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java => tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java (51%) rename {plugs-maven-plugin => tools/plugs-maven-plugin}/src/test/resources/simpliest/pom.xml (88%) create mode 100644 tools/plugs-packager-core/pom.xml create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java (56%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/Packager.java (58%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/PackagerFactory.java (80%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/ArchiveWriter.java (95%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/ArchiveWriterEvent.java (88%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/ArchiveWriterListener.java (87%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/CrcAndSize.java (97%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/EntryTransformer.java (94%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/EntryWriter.java (95%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/FileUtils.java (98%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/IdentityEntryTransformer.java (94%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/InputStreamEntryWriter.java (96%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/JarWriter.java (91%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/LibrariesCollector.java (80%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/LibraryHasBeenWritten.java (82%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/LibraryUnpackHandler.java (90%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/NeverUnpackHandler.java (88%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/UnpackHandler.java (94%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/WritableLibraries.java (76%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar}/ZipHeaderPeekInputStream.java (97%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest}/ManifestBuilder.java (64%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model}/Filter.java (73%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/model/FilterableDependency.java (75%) rename plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java (81%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/model/Libraries.java (95%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/model/Library.java (92%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/model/LibraryCallback.java (95%) rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/model/LibraryScope.java (80%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/model/StandardLibraries.java (94%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java rename {plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter => tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core}/package-info.java (93%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java create mode 100644 tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named create mode 100644 tools/pom.xml diff --git a/boms-parent/maven-plugin-bom/pom.xml b/boms-parent/maven-plugin-bom/pom.xml index c4b0efc..123fe91 100644 --- a/boms-parent/maven-plugin-bom/pom.xml +++ b/boms-parent/maven-plugin-bom/pom.xml @@ -32,6 +32,7 @@ Plugs :: BOM :: Maven Plugin BOM + 3.3.9 @@ -86,6 +87,12 @@ 3.3.0 test + + org.apache.maven + maven-embedder + ${maven-plugin.version} + test + diff --git a/plugs-core/src/main/java/pl/wavesoftware/plugs/core/AbstractFrameworkProducer.java b/plugs-core/src/main/java/pl/wavesoftware/plugs/core/AbstractFrameworkProducer.java index 633cb09..c6d9bbe 100644 --- a/plugs-core/src/main/java/pl/wavesoftware/plugs/core/AbstractFrameworkProducer.java +++ b/plugs-core/src/main/java/pl/wavesoftware/plugs/core/AbstractFrameworkProducer.java @@ -47,7 +47,7 @@ private static void initialize( Framework framework ) { tryToExecute( - () -> framework.init(listeners.toJavaArray(FrameworkListener.class)), + () -> framework.init(listeners.toJavaArray(FrameworkListener[]::new)), "20190105:114131" ); } diff --git a/plugs-core/src/test/java/pl/wavesoftware/plugs/core/PlugsOsgiContainerTest.java b/plugs-core/src/test/java/pl/wavesoftware/plugs/core/PlugsOsgiContainerTest.java index e88c804..c46ad8c 100644 --- a/plugs-core/src/test/java/pl/wavesoftware/plugs/core/PlugsOsgiContainerTest.java +++ b/plugs-core/src/test/java/pl/wavesoftware/plugs/core/PlugsOsgiContainerTest.java @@ -153,7 +153,7 @@ void disposeWithoutEnoughTime() throws InterruptedException, BundleException { } private static String takeFirst2Lines(LogMessage message) { - return List.of(message.text.split("\n")) + return List.of(message.text.replace("\r\n", "\n").split("\n")) .take(2) .intersperse("\n") .foldLeft(new StringBuilder(), StringBuilder::append) diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java deleted file mode 100644 index edc383c..0000000 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/PlugsMojoException.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.maven.generator.model; - -import org.apache.maven.plugin.MojoExecutionException; - -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -/** - * A standard Plugs mojo exception - * - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -public final class PlugsMojoException extends MojoExecutionException { - private static final long serialVersionUID = 20190115004728L; - - @Nullable - private final ArrayList failures; - - public PlugsMojoException(List failures) { - super("Multiple reasons of failure"); - this.failures = new ArrayList<>(failures); - } - - public PlugsMojoException(String message, Throwable cause) { - super(message, cause); - failures = null; - } - - public Optional> getFailures() { - return Optional.ofNullable(failures); - } -} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java deleted file mode 100644 index 0222880..0000000 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilderImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.maven.generator.packager; - -import io.vavr.collection.Set; -import org.apache.maven.artifact.Artifact; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import pl.wavesoftware.plugs.maven.generator.model.Library; - -import javax.inject.Named; -import java.util.jar.Attributes; -import java.util.jar.Manifest; - -import static pl.wavesoftware.plugs.maven.generator.packager.Constants.PLUGS_VERSION_ATTRIBUTE; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -@Named -final class ManifestBuilderImpl implements ManifestBuilder { - - private static final Logger LOGGER = - LoggerFactory.getLogger(ManifestBuilderImpl.class); - - @Override - public Manifest buildManifest( - Artifact artifact, - Set dependencies, - Set imports - ) { - Manifest manifest = new Manifest(); - Attributes attributes = manifest.getMainAttributes(); - String plugsVersion = getClass().getPackage().getImplementationVersion(); - - attributes.putValue("Manifest-Version", "1.0"); - attributes.putValue(PLUGS_VERSION_ATTRIBUTE, plugsVersion); - - LOGGER.warn("Define dependencies"); - LOGGER.warn("Define imports"); - LOGGER.warn("Define sources"); - LOGGER.warn("Define name & version"); - - return manifest; - } -} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java b/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java deleted file mode 100644 index 98d3050..0000000 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerImpl.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.maven.generator.packager; - -import io.vavr.Lazy; -import io.vavr.collection.HashSet; -import org.apache.maven.artifact.Artifact; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import pl.wavesoftware.plugs.maven.generator.filter.Filter; -import pl.wavesoftware.plugs.maven.generator.model.ArtifactsLibraries; -import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; -import pl.wavesoftware.plugs.maven.generator.model.Libraries; -import pl.wavesoftware.plugs.maven.generator.model.Library; -import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; - -import javax.annotation.Nullable; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Set; -import java.util.jar.JarFile; -import java.util.jar.Manifest; - -import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; -import static pl.wavesoftware.plugs.maven.generator.packager.Constants.PLUGS_VERSION_ATTRIBUTE; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -final class PackagerImpl implements Packager { - - private static final Logger LOGGER = - LoggerFactory.getLogger(PackagerImpl.class); - - private final ExecutionConfiguration configuration; - private final Filter filter; - private final ManifestBuilder manifestBuilder; - private final Lazy lazyTargetFile; - private final Lazy lazySourceArtifact; - - PackagerImpl( - ExecutionConfiguration configuration, - Filter filter, - ManifestBuilder manifestBuilder - ) { - this.configuration = configuration; - this.filter = filter; - this.manifestBuilder = manifestBuilder; - lazyTargetFile = Lazy.of(this::calculateTargetFile); - lazySourceArtifact = Lazy.of(this::calculateSourceArtifact); - } - - @Override - public boolean needsRepackaging() { - return tryToExecute(() -> !alreadyRepackaged(), "20190116:000119"); - } - - @Override - public void repackage() throws PlugsMojoException { - Artifact source = getSourceArtifact(); - File target = getTargetFile(); - Set artifacts = filter.filterDependencies( - configuration.getMavenProject().getArtifacts() - ); - Libraries libraries = new ArtifactsLibraries( - artifacts, - new ArrayList<>(), - configuration.getLog() - ); - - try { - repackage(source, target, libraries); - } catch (IOException ex) { - throw new PlugsMojoException(ex.getMessage(), ex); - } - } - - @Override - public File getTargetFile() { - return lazyTargetFile.get(); - } - - @Override - public Artifact getSourceArtifact() { - return lazySourceArtifact.get(); - } - - private boolean alreadyRepackaged() throws IOException { - File targetFile = getTargetFile(); - if (!targetFile.isFile()) { - return false; - } - try (JarFile jarFile = new JarFile(targetFile)) { - Manifest manifest = jarFile.getManifest(); - LOGGER.warn("Better decision of noop is needed, based on source hash."); - return manifest != null - && manifest.getMainAttributes() - .getValue(PLUGS_VERSION_ATTRIBUTE) != null; - } - } - - private void repackage( - Artifact source, - File destination, - Libraries libraries - ) throws IOException { - destination = destination.getAbsoluteFile(); - Files.deleteIfExists(destination.toPath()); - WritableLibraries writeableLibraries = new WritableLibraries(libraries); - LibrariesCollector collector = new LibrariesCollector(); - try (JarWriter writer = new JarWriter(destination)) { - writer.addListener( - LibraryHasBeenWritten.class, - event -> collector.collect(event.getLibrary()) - ); - writeableLibraries.write(writer); - LOGGER.warn("Write source artifact compiled sources"); - - LOGGER.warn("Create real imports, from provided scope"); - HashSet imports = HashSet.empty(); - Manifest manifest = manifestBuilder.buildManifest( - source, - collector.getCollected(), - imports - ); - writer.writeManifest(manifest); - } - } - - private Artifact calculateSourceArtifact() { - @Nullable - Artifact sourceArtifact = getArtifact(configuration.getClassifier()); - return (sourceArtifact != null) - ? sourceArtifact - : configuration.getMavenProject().getArtifact(); - } - - private File calculateTargetFile() { - String classifier = configuration.getClassifier().trim(); - if (!classifier.isEmpty() && !classifier.startsWith("-")) { - classifier = "-" + classifier; - } - //noinspection RedundantIfStatement - if (!configuration.getOutputDirectory().exists()) { - assert configuration.getOutputDirectory().mkdirs(); - } - return new File( - configuration.getOutputDirectory(), - configuration.getFinalName() - + classifier + "." - + configuration.getMavenProject() - .getArtifact() - .getArtifactHandler() - .getExtension() - ); - } - - @Nullable - private Artifact getArtifact(String classifier) { - for (Artifact attachedArtifact : configuration.getMavenProject().getAttachedArtifacts()) { - if (classifier.equals(attachedArtifact.getClassifier()) - && attachedArtifact.getFile() != null - && attachedArtifact.getFile().isFile()) { - return attachedArtifact; - } - } - return null; - } -} diff --git a/plugs-maven-plugin/src/main/resources/log4j2.xml b/plugs-maven-plugin/src/main/resources/log4j2.xml deleted file mode 100644 index 2302b4e..0000000 --- a/plugs-maven-plugin/src/main/resources/log4j2.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - %hightlight{%5p} %-40.40c{1.} %m%n%ex - - - - - - - - - - - - diff --git a/pom.xml b/pom.xml index 97e2757..920d384 100644 --- a/pom.xml +++ b/pom.xml @@ -62,10 +62,10 @@ plugs-core plugs-felix plugs-spring - plugs-maven-plugin boms-parent examples testing + tools @@ -99,8 +99,7 @@ UTF-8 UTF-8 - ${project.build.directory}/sonar - + ${project.build.directory}/sonar https://sonar.wavesoftware.pl jacoco 8 @@ -113,8 +112,8 @@ ${skipTests} - 2.1.1.RELEASE - 6.0.1 + 2.1.2.RELEASE + 6.0.2 3.6.0 @@ -135,7 +134,7 @@ org.apache.felix org.apache.felix.framework - 6.0.1 + ${felix.version} org.osgi @@ -155,19 +154,29 @@ io.vavr vavr - 0.9.2 + 0.10.0 pl.wavesoftware eid-exceptions 2.0.0 + + com.github.zafarkhaja + java-semver + 0.9.0 + + + org.apache.commons + commons-compress + 1.18 + org.awaitility awaitility - 3.1.5 + 3.1.6 test @@ -185,7 +194,7 @@ org.sonarsource.scanner.maven sonar-maven-plugin - 3.5.0.1254 + 3.6.0.1398 com.manamind.jgitflow @@ -201,7 +210,7 @@ org.jacoco jacoco-maven-plugin - 0.8.2 + 0.8.3 jacoco-initialize @@ -371,8 +380,7 @@ preview - true - + true true issues.json @@ -389,7 +397,7 @@ default - post-integration-test + verify sonar diff --git a/testing/src/main/resources/log4j2-test.xml b/testing/src/main/resources/log4j2-test.xml index 154a4b9..8306854 100644 --- a/testing/src/main/resources/log4j2-test.xml +++ b/testing/src/main/resources/log4j2-test.xml @@ -17,12 +17,15 @@ - %hl{%5p} %hl{%d{HH:mm:ss.SSS}}{faint} %hl{[%10.10t]}{yellow} %hl{%-40.40c{1.}}{cyan} %hl{:}{faint} %m%n%hl{%ex} + %hl{%d{HH:mm:ss.SSS}}{faint} + %hl{[%10.10t]}{yellow} + %hl{%-40.40c{1.}}{cyan} + %hl{:}{faint} %m%n%hl{%xEx} + %hl{%5p} ${DATE} ${THREAD} ${CLASS} ${MSG} - + diff --git a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java index 4e2a924..8c422d1 100644 --- a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java +++ b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java @@ -34,6 +34,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pl.wavesoftware.plugs.testing.log4j2.CollectorManager.CollectedEvent; import java.util.stream.Stream; @@ -69,7 +70,7 @@ void format() { // then assertThat(manager.getCollected()) .hasSize(EXPECTED_SIZE) - .extracting(CollectorManager.CollectedEvent::getFormattedMessage) + .extracting(this::formattedMessageOf) .containsExactly( "\u001B[32m INFO\u001B[0;39m \u001B[33m[ main]\u001B[0;39m " + "\u001B[36mp.w.p.t.l.HighlightColorConverterTest " + @@ -79,6 +80,10 @@ void format() { "\u001B[0;39m \u001B[37;2m:\u001B[0;39m error\n" ); } + + private String formattedMessageOf(CollectedEvent event) { + return event.getFormattedMessage().replace("\r\n", "\n"); + } } @ExtendWith(MockitoExtension.class) diff --git a/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml similarity index 85% rename from plugs-maven-plugin/pom.xml rename to tools/plugs-maven-plugin/pom.xml index 8eeb41a..6bf93e8 100644 --- a/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -22,14 +22,14 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - plugs-parent - pl.wavesoftware.plugs + plugs-tools-parent + pl.wavesoftware.plugs.tools 0.1.0-SNAPSHOT plugs-maven-plugin maven-plugin - Plugs :: Plugs Maven Plugin + Plugs :: Tools :: Plugs Maven Plugin @@ -50,6 +50,11 @@ ${project.version} pom + + pl.wavesoftware.plugs.tools + plugs-packager-core + ${project.version} + org.apache.maven maven-plugin-api @@ -122,5 +127,19 @@ maven-plugin-testing-harness test + + org.apache.maven + maven-embedder + test + + + + + + src/test/resources + true + + + diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java similarity index 62% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java index 30c64b3..7134b18 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojo.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.plugs.tools.maven.plugin; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -26,15 +26,15 @@ import org.apache.maven.project.MavenProjectHelper; import org.apiguardian.api.API; import org.apiguardian.api.API.Status; -import pl.wavesoftware.plugs.maven.generator.filter.Filter; -import pl.wavesoftware.plugs.maven.generator.filter.FilterFactory; -import pl.wavesoftware.plugs.maven.generator.model.BuildFailure; -import pl.wavesoftware.plugs.maven.generator.model.Exclude; -import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; -import pl.wavesoftware.plugs.maven.generator.model.Include; -import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; -import pl.wavesoftware.plugs.maven.generator.packager.Packager; -import pl.wavesoftware.plugs.maven.generator.packager.PackagerFactory; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; +import pl.wavesoftware.plugs.tools.packager.core.Packager; +import pl.wavesoftware.plugs.tools.packager.core.model.CodeBlock; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; import javax.inject.Inject; import java.io.File; @@ -54,19 +54,18 @@ ) final class PackagePlugMojo extends AbstractMojo { - @SuppressWarnings("WeakerAccess") @API(status = Status.STABLE) public static final String GOAL = "package"; /** - * The package factory + * The package factories */ - private final PackagerFactory packagerFactory; + private final PackagerFactories factories; /** - * The filter factory + * The logger */ - private final FilterFactory filterFactory; + private final Logger logger; /** * Maven's project helper @@ -156,83 +155,93 @@ final class PackagePlugMojo extends AbstractMojo { @Inject PackagePlugMojo( - PackagerFactory packagerFactory, - FilterFactory filterFactory, + PackagerFactories factories, MavenProjectHelper projectHelper ) { - this.packagerFactory = packagerFactory; - this.filterFactory = filterFactory; + this.factories = factories; + this.logger = factories.logger().create(this::getLog); this.projectHelper = projectHelper; } @Override public void execute() throws MojoExecutionException { if ("pom".equals(project.getPackaging())) { - getLog().debug(GOAL + " goal could not be applied to pom project."); + logger.info( + "Skipping, {} goal could not be applied to pom project.", + GOAL + ); return; } if (this.skip) { - getLog().debug("skipping packaging as per configuration."); + logger.info("Skipping packaging as per configuration."); return; } - build(); + maybePackage(); } - private void build() throws MojoExecutionException { - Filter filter = filterFactory.create(includes, excludes); - ExecutionConfiguration configuration = getConfiguration(); - Packager packager = packagerFactory.create(configuration, filter); - if (packager.needsRepackaging()) { - buildAndReport(packager, configuration); - } else { - getLog().info("Plug package " + packager.getTargetFile() + " is up-to-date."); - } + private void maybePackage() throws MojoExecutionException { + Filter filter = factories.filter().create(includes, excludes); + PackagerConfiguration configuration = createConfiguration(); + Packager packager = factories.packager().create( + configuration, filter + ); + PackagerCoordinates coordinates = configuration.coordinates(); + logger.info("Building plug: {}", coordinates.targetPath()); + repackageAndReport(packager, configuration).or(() -> logger.info( + "Plug package {} is up-to-date.", + coordinates.targetPath().getFileName() + )); + attachArtifactIfNeeded(configuration); } - private void buildAndReport( + @SuppressWarnings("squid:S1162") + private MojoOr repackageAndReport( Packager packager, - ExecutionConfiguration configuration - ) throws MojoExecutionException { - try { - packager.repackage(); - getLog().info("Building of " + packager.getTargetFile() + " was successful."); - attachIfNeeded(packager, configuration); - } catch (PlugsMojoException ex) { - getLog().error("Building of " + packager.getTargetFile() + " has failed."); - ex.getFailures().ifPresent(failures -> { - getLog().error("Reasons of failure:"); - for (BuildFailure failure : failures) { - getLog().error(" * " + failure.describe()); - } - }); - throw ex; - } + PackagerConfiguration configuration + ) { + PackagerCoordinates coordinates = configuration.coordinates(); + return codeBlock -> { + try { + packager.repackage().or(codeBlock); + logger.debug("Building of {} was successful.", coordinates.targetPath()); + } catch (RepackageFailed ex) { + logger.error( + "Building of {} has failed: {}", + coordinates.targetPath(), + ex.getMessage() + ); + throw new MojoExecutionException("Repackage FAILURE!", ex); + } + }; } - private void attachIfNeeded( - Packager packager, - ExecutionConfiguration configuration - ) { + private void attachArtifactIfNeeded(PackagerConfiguration configuration) { + PackagerCoordinates coordinates = configuration.coordinates(); if (configuration.shouldAttach()) { projectHelper.attachArtifact( project, - packager.getTargetFile(), - configuration.getClassifier() + coordinates.targetPath().toFile(), + configuration.project().classifier() ); - getLog().info("Artifact attached to the build: " + packager.getTargetFile()); + logger.debug("Artifact attached to the build: {}", coordinates.targetPath()); } else { - getLog().debug("Skipping attach of artifact: " + packager.getTargetFile()); + logger.info("Skipping attaching of artifact: {}", + coordinates.targetPath()); } } - private ExecutionConfiguration getConfiguration() { - return new DefaultExecutionConfiguration( - getLog(), + private PackagerConfiguration createConfiguration() { + return factories.configuration().create( project, + logger, classifier, attach, outputDirectory, finalName ); } + + private interface MojoOr { + void or(CodeBlock codeBlock) throws MojoExecutionException; + } } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java new file mode 100644 index 0000000..b551a66 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin; + +import pl.wavesoftware.plugs.tools.maven.plugin.filter.FilterFactory; +import pl.wavesoftware.plugs.tools.maven.plugin.io.LoggerFactory; +import pl.wavesoftware.plugs.tools.maven.plugin.mapper.PackagerConfigurationFactory; +import pl.wavesoftware.plugs.tools.packager.core.PackagerFactory; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface PackagerFactories { + PackagerFactory packager(); + FilterFactory filter(); + LoggerFactory logger(); + PackagerConfigurationFactory configuration(); +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java new file mode 100644 index 0000000..10036fc --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin; + +import pl.wavesoftware.plugs.tools.maven.plugin.filter.FilterFactory; +import pl.wavesoftware.plugs.tools.maven.plugin.io.LoggerFactory; +import pl.wavesoftware.plugs.tools.maven.plugin.mapper.PackagerConfigurationFactory; +import pl.wavesoftware.plugs.tools.packager.core.PackagerFactory; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class PackagerFactoriesImpl implements PackagerFactories { + + private final PackagerFactory packagerFactory; + private final FilterFactory filterFactory; + private final LoggerFactory loggerFactory; + private final PackagerConfigurationFactory configurationFactory; + + @Inject + PackagerFactoriesImpl( + PackagerFactory packagerFactory, + FilterFactory filterFactory, + LoggerFactory loggerFactory, + PackagerConfigurationFactory configurationFactory + ) { + this.packagerFactory = packagerFactory; + this.filterFactory = filterFactory; + this.loggerFactory = loggerFactory; + this.configurationFactory = configurationFactory; + } + + @Override + public PackagerFactory packager() { + return packagerFactory; + } + + @Override + public FilterFactory filter() { + return filterFactory; + } + + @Override + public LoggerFactory logger() { + return loggerFactory; + } + + @Override + public PackagerConfigurationFactory configuration() { + return configurationFactory; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java similarity index 88% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java index cc52257..84c4101 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/AbstractDependencyFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.maven.plugin.filter; import org.apache.maven.artifact.Artifact; import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; -import pl.wavesoftware.plugs.maven.generator.model.FilterableDependency; +import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; import java.util.ArrayList; import java.util.Collections; @@ -75,16 +75,16 @@ final boolean equals( Artifact artifact, FilterableDependency dependency ) { - if (!dependency.getGroupId().equals(artifact.getGroupId())) { + if (!dependency.groupId().equals(artifact.getGroupId())) { return false; } - if (!dependency.getArtifactId().equals(artifact.getArtifactId())) { + if (!dependency.artifactId().equals(artifact.getArtifactId())) { return false; } - return (dependency.getClassifier() == null + return (dependency.classifier() == null || ( artifact.getClassifier() != null - && dependency.getClassifier().equals(artifact.getClassifier()) + && dependency.classifier().equals(artifact.getClassifier()) ) ); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java similarity index 86% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java index d7c0aa3..ea81729 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/ExcludeFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.maven.plugin.filter; import org.apache.maven.artifact.Artifact; -import pl.wavesoftware.plugs.maven.generator.model.Exclude; -import pl.wavesoftware.plugs.maven.generator.model.FilterableDependency; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; +import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; import java.util.List; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java similarity index 72% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java index c7fc6ca..6f98294 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/FilterFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.maven.plugin.filter; -import pl.wavesoftware.plugs.maven.generator.model.Exclude; -import pl.wavesoftware.plugs.maven.generator.model.Include; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; import javax.annotation.Nullable; import java.util.List; @@ -28,9 +29,7 @@ */ public interface FilterFactory { Filter create( - @Nullable - List includes, - @Nullable - List excludes + @Nullable List includes, + @Nullable List excludes ); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java similarity index 87% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java index 60cf56d..1276a0d 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/IncludeFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.maven.plugin.filter; import org.apache.maven.artifact.Artifact; import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; -import pl.wavesoftware.plugs.maven.generator.model.FilterableDependency; -import pl.wavesoftware.plugs.maven.generator.model.Include; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; +import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; import java.util.List; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java similarity index 53% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java index bd9b48a..c068643 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java @@ -14,36 +14,43 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.maven.plugin.filter; -import org.apache.maven.artifact.Artifact; +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; -import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; - -import java.util.LinkedHashSet; -import java.util.Set; +import pl.wavesoftware.plugs.tools.maven.plugin.mapper.ArtifactMapper; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -final class DefaultFilter implements Filter { +final class MavenFilter implements Filter { private final FilterArtifacts filters; + private final ArtifactMapper artifactMapper; - DefaultFilter(FilterArtifacts filters) { + MavenFilter(FilterArtifacts filters, ArtifactMapper artifactMapper) { this.filters = filters; + this.artifactMapper = artifactMapper; } @Override public Set filterDependencies(Set dependencies) - throws PlugsMojoException { + throws RepackageFailed { try { - Set filtered = new LinkedHashSet<>(dependencies); - filtered.retainAll(filters.filter(dependencies)); - return filtered; + Set delta = HashSet.ofAll( + filters.filter(dependencies.map(artifactMapper::mavenize).toJavaSet()) + ).map(artifactMapper::generalize); + return dependencies.retainAll(delta); } catch (ArtifactFilterException ex) { - throw new PlugsMojoException(ex.getMessage(), ex); + throw new RepackageFailed( + "Can't filter Maven dependencies using provided filters", + ex + ); } } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java similarity index 70% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java index dfc9d95..7c40283 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/DefaultFilterFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java @@ -14,13 +14,16 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.maven.plugin.filter; import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; -import pl.wavesoftware.plugs.maven.generator.model.Exclude; -import pl.wavesoftware.plugs.maven.generator.model.Include; +import pl.wavesoftware.plugs.tools.maven.plugin.mapper.ArtifactMapper; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; import javax.annotation.Nullable; +import javax.inject.Inject; import javax.inject.Named; import java.util.List; @@ -29,14 +32,22 @@ * @since 0.1.0 */ @Named -final class DefaultFilterFactory implements FilterFactory { +final class MavenFilterFactory implements FilterFactory { + + private final ArtifactMapper artifactMapper; + + @Inject + MavenFilterFactory(ArtifactMapper artifactMapper) { + this.artifactMapper = artifactMapper; + } + @Override public Filter create( @Nullable List includes, @Nullable List excludes ) { FilterArtifacts filters = getFilters(includes, excludes); - return new DefaultFilter(filters); + return new MavenFilter(filters, artifactMapper); } private static FilterArtifacts getFilters( diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java new file mode 100644 index 0000000..14e26c7 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.maven.plugin.filter; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/LoggerFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/LoggerFactory.java new file mode 100644 index 0000000..499c4f7 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/LoggerFactory.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.io; + +import org.apache.maven.plugin.logging.Log; +import org.slf4j.Logger; + +import java.util.function.Supplier; + +/** + * Creates a SLF4J compatible logger based on Maven logger. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface LoggerFactory { + /** + * Creates a logger from Maven log. + * + * @param log a maven log supplier + * @return a SLF4J compatible logger + */ + Logger create(Supplier log); +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLogger.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLogger.java new file mode 100644 index 0000000..9ce94c1 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLogger.java @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.io; + +import io.vavr.Lazy; +import org.apache.maven.plugin.logging.Log; +import org.slf4j.helpers.MarkerIgnoringBase; + +import java.util.function.Supplier; + +import static org.slf4j.helpers.MessageFormatter.arrayFormat; +import static org.slf4j.helpers.MessageFormatter.format; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MavenLogger extends MarkerIgnoringBase { + private static final long serialVersionUID = 20190207213304L; + private final Lazy log; + + MavenLogger(Supplier logSupplier) { + this.log = Lazy.of(logSupplier); + name = Log.class.getName(); + } + + @Override + public boolean isTraceEnabled() { + return isDebugEnabled(); + } + + @Override + public void trace(String msg) { + debug(msg); + } + + @Override + public void trace(String format, Object arg) { + debug(format, arg); + } + + @Override + public void trace(String format, Object arg1, Object arg2) { + debug(format, arg1, arg2); + } + + @Override + public void trace(String format, Object... arguments) { + debug(format, arguments); + } + + @Override + public void trace(String msg, Throwable throwable) { + debug(msg, throwable); + } + + @Override + public boolean isDebugEnabled() { + return log.get().isDebugEnabled(); + } + + @Override + public void debug(String msg) { + if (isDebugEnabled()) { + log.get().debug(msg); + } + } + + @Override + public void debug(String format, Object arg) { + if (isDebugEnabled()) { + log.get().debug(format(format, arg).getMessage()); + } + } + + @Override + public void debug(String format, Object arg1, Object arg2) { + if (isDebugEnabled()) { + log.get().debug(format(format, arg1, arg2).getMessage()); + } + } + + @Override + public void debug(String format, Object... arguments) { + if (isDebugEnabled()) { + log.get().debug(arrayFormat(format, arguments).getMessage()); + } + } + + @Override + public void debug(String msg, Throwable throwable) { + if (isDebugEnabled()) { + log.get().debug(msg, throwable); + } + } + + @Override + public boolean isInfoEnabled() { + return log.get().isInfoEnabled(); + } + + @Override + public void info(String msg) { + if (isInfoEnabled()) { + log.get().info(msg); + } + } + + @Override + public void info(String format, Object arg) { + if (isInfoEnabled()) { + log.get().info(format(format, arg).getMessage()); + } + } + + @Override + public void info(String format, Object arg1, Object arg2) { + if (isInfoEnabled()) { + log.get().info(format(format, arg1, arg2).getMessage()); + } + } + + @Override + public void info(String format, Object... arguments) { + if (isInfoEnabled()) { + log.get().info(arrayFormat(format, arguments).getMessage()); + } + } + + @Override + public void info(String msg, Throwable throwable) { + if (isInfoEnabled()) { + log.get().info(msg, throwable); + } + } + + @Override + public boolean isWarnEnabled() { + return log.get().isWarnEnabled(); + } + + @Override + public void warn(String msg) { + if (isWarnEnabled()) { + log.get().warn(msg); + } + } + + @Override + public void warn(String format, Object arg) { + if (isWarnEnabled()) { + log.get().warn(format(format, arg).getMessage()); + } + } + + @Override + public void warn(String format, Object... arguments) { + if (isWarnEnabled()) { + log.get().warn(arrayFormat(format, arguments).getMessage()); + } + } + + @Override + public void warn(String format, Object arg1, Object arg2) { + if (isWarnEnabled()) { + log.get().warn(format(format, arg1, arg2).getMessage()); + } + } + + @Override + public void warn(String msg, Throwable throwable) { + if (isWarnEnabled()) { + log.get().warn(msg, throwable); + } + } + + @Override + public boolean isErrorEnabled() { + return log.get().isErrorEnabled(); + } + + @Override + public void error(String msg) { + if (isErrorEnabled()) { + log.get().error(msg); + } + } + + @Override + public void error(String format, Object arg) { + if (isErrorEnabled()) { + log.get().error(format(format, arg).getMessage()); + } + } + + @Override + public void error(String format, Object arg1, Object arg2) { + if (isErrorEnabled()) { + log.get().error(format(format, arg1, arg2).getMessage()); + } + } + + @Override + public void error(String format, Object... arguments) { + if (isErrorEnabled()) { + log.get().error(arrayFormat(format, arguments).getMessage()); + } + } + + @Override + public void error(String msg, Throwable throwable) { + if (isErrorEnabled()) { + log.get().error(msg, throwable); + } + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerFactory.java similarity index 70% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerFactory.java index d39df6b..18598cd 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ExecutionConfiguration.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerFactory.java @@ -14,22 +14,22 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.maven.plugin.io; import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; +import org.slf4j.Logger; -import java.io.File; +import javax.inject.Named; +import java.util.function.Supplier; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface ExecutionConfiguration { - Log getLog(); - String getFinalName(); - String getClassifier(); - boolean shouldAttach(); - File getOutputDirectory(); - MavenProject getMavenProject(); +@Named +final class MavenLoggerFactory implements LoggerFactory { + @Override + public Logger create(Supplier log) { + return new MavenLogger(log); + } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/package-info.java similarity index 93% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/package-info.java index a1d8ac5..eb886bd 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/package-info.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/io/package-info.java @@ -19,6 +19,6 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.maven.plugin.io; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java similarity index 71% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java index e8dcc61..6ea8416 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Constants.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java @@ -14,17 +14,15 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -final class Constants { - static final String PLUGS_VERSION_ATTRIBUTE = "Plugs-Version"; - static final String LIBRARY_DESTINATION = "PLUGS-INF/lib/"; - - private Constants() { - // non reachable - } +public interface ArtifactMapper { + Artifact generalize(org.apache.maven.artifact.Artifact artifact); + org.apache.maven.artifact.Artifact mavenize(Artifact artifact); } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java new file mode 100644 index 0000000..298b5d7 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; + +import javax.inject.Named; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class ArtifactMapperImpl implements ArtifactMapper { + @Override + public Artifact generalize(org.apache.maven.artifact.Artifact artifact) { + return new MavenArtifact(artifact); + } + + @Override + public org.apache.maven.artifact.Artifact mavenize(Artifact artifact) { + if (artifact instanceof MavenArtifact) { + return ((MavenArtifact) artifact).getDelegate(); + } + throw new UnsupportedOperationException( + "Not supported artifact type: " + artifact.getClass() + ); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java new file mode 100644 index 0000000..e710b40 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import com.github.zafarkhaja.semver.Version; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; + +import java.nio.file.Path; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MavenArtifact implements Artifact { + private final org.apache.maven.artifact.Artifact delegate; + + MavenArtifact(org.apache.maven.artifact.Artifact delegate) { + this.delegate = delegate; + } + + org.apache.maven.artifact.Artifact getDelegate() { + return delegate; + } + + @Override + public String name() { + return getDelegate().getArtifactId(); + } + + @Override + public Version version() { + return Version.valueOf(getDelegate().getVersion()); + } + + @Override + public Path path() { + return getDelegate().getFile().toPath(); + } + + @Override + public ArtifactType type() { + return ArtifactType.fromPackging(getDelegate().getType()); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java new file mode 100644 index 0000000..224bbd8 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import io.vavr.Lazy; +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import org.apache.maven.project.MavenProject; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; + +import java.nio.file.Path; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MavenBackedProject implements Project { + private final ArtifactMapper artifactMapper; + private final MavenProject mavenProject; + private final Path path; + private final String finalName; + private final String classifier; + + private final Lazy> dependenciesLazy; + private final Lazy> importsLazy; + + MavenBackedProject( + ArtifactMapper artifactMapper, + MavenProject mavenProject, + Path path, + String finalName, + String classifier + ) { + this.artifactMapper = artifactMapper; + this.mavenProject = mavenProject; + this.path = path; + this.finalName = finalName; + this.classifier = classifier; + + dependenciesLazy = Lazy.of(this::calculateDependencies); + importsLazy = Lazy.of(this::calculateImports); + } + + @Override + public Artifact mainArtifact() { + return artifactMapper.generalize(mavenProject.getArtifact()); + } + + @Override + public Path outputPath() { + return path; + } + + @Override + public String finalName() { + return finalName; + } + + @Override + public String classifier() { + return classifier; + } + + @Override + public Set dependencies() { + return dependenciesLazy.get(); + } + + @Override + public Set imports() { + return importsLazy.get(); + } + + private Set calculateDependencies() { + return HashSet + .ofAll(mavenProject.getArtifacts()) + .reject(MavenBackedProject::hasProvidedScope) + .map(artifactMapper::generalize); + } + + private Set calculateImports() { + return HashSet + .ofAll(mavenProject.getArtifacts()) + .filter(MavenBackedProject::hasProvidedScope) + .map(artifactMapper::generalize); + } + + private static boolean hasProvidedScope(org.apache.maven.artifact.Artifact artifact) { + return artifact.getScope().equals(org.apache.maven.artifact.Artifact.SCOPE_SYSTEM); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java new file mode 100644 index 0000000..e1e8a53 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import io.vavr.collection.Set; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.maven.plugin.model.MavenLibraries; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.core.spi.LibrariesFactory; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class MavenLibrariesFactory implements LibrariesFactory { + + private final ArtifactMapper artifactMapper; + + @Inject + MavenLibrariesFactory(ArtifactMapper artifactMapper) { + this.artifactMapper = artifactMapper; + } + + @Override + public Libraries create(Set artifacts, Logger logger) { + return new MavenLibraries( + artifacts.map(artifactMapper::mavenize), + null, + logger + ); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java similarity index 50% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java index 17b973e..8ebc379 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/DefaultExecutionConfiguration.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java @@ -14,57 +14,57 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.plugs.tools.maven.plugin.mapper; -import org.apache.maven.plugin.logging.Log; +import io.vavr.Lazy; import org.apache.maven.project.MavenProject; -import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; import java.io.File; +import java.util.function.Supplier; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -final class DefaultExecutionConfiguration - implements ExecutionConfiguration { +final class MavenPackagerConfiguration implements PackagerConfiguration { - private final Log log; - private final MavenProject project; + private final ArtifactMapper artifactMapper; + private final Logger logger; + private final MavenProject mavenProject; + private final Supplier project; private final String classifier; private final boolean attach; private final File outputDirectory; private final String finalName; + private final PackagerCoordinates coordinates; - DefaultExecutionConfiguration( - Log log, + MavenPackagerConfiguration( + ArtifactMapper artifactMapper, + Logger logger, MavenProject project, String classifier, boolean attach, File outputDirectory, String finalName ) { - this.log = log; - this.project = project; + this.artifactMapper = artifactMapper; + this.logger = logger; + this.mavenProject = project; this.classifier = classifier; this.attach = attach; this.outputDirectory = outputDirectory; this.finalName = finalName; + this.project = Lazy.of(this::calculateProject); + this.coordinates = new MavenPackagerCoordinates(this.project); } @Override - public Log getLog() { - return log; - } - - @Override - public String getFinalName() { - return finalName; - } - - @Override - public String getClassifier() { - return classifier; + public Logger logger() { + return logger; } @Override @@ -73,12 +73,22 @@ public boolean shouldAttach() { } @Override - public File getOutputDirectory() { - return outputDirectory; + public PackagerCoordinates coordinates() { + return coordinates; } @Override - public MavenProject getMavenProject() { - return project; + public Project project() { + return project.get(); + } + + private Project calculateProject() { + return new MavenBackedProject( + artifactMapper, + mavenProject, + outputDirectory.toPath(), + finalName, + classifier + ); } } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java new file mode 100644 index 0000000..8de55e6 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import org.apache.maven.project.MavenProject; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.File; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class MavenPackagerConfigurationFactory implements PackagerConfigurationFactory { + + private final ArtifactMapper artifactMapper; + + @Inject + MavenPackagerConfigurationFactory(ArtifactMapper artifactMapper) { + this.artifactMapper = artifactMapper; + } + + @Override + public PackagerConfiguration create( + MavenProject project, + Logger logger, + String classifier, + boolean attach, + File outputDirectory, + String finalName + ) { + return new MavenPackagerConfiguration( + artifactMapper, + logger, + project, + classifier, + attach, + outputDirectory, + finalName + ); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java new file mode 100644 index 0000000..1af4faf --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import io.vavr.Lazy; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; + +import java.nio.file.Path; +import java.util.function.Supplier; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MavenPackagerCoordinates implements PackagerCoordinates { + private final Supplier projectSupplier; + private final Lazy lazyTargetPath; + private final Lazy lazySourceArtifact; + + MavenPackagerCoordinates(Supplier project) { + this.projectSupplier = project; + lazyTargetPath = Lazy.of(this::calculateTargetPath); + lazySourceArtifact = Lazy.of(this::calculateSourceArtifact); + } + + @Override + public Artifact sourceArtifact() { + return lazySourceArtifact.get(); + } + + @Override + public Path targetPath() { + return lazyTargetPath.get(); + } + + private Artifact calculateSourceArtifact() { + return projectSupplier.get().mainArtifact(); + } + + private Path calculateTargetPath() { + Project project = this.projectSupplier.get(); + String classifier = project.classifier().trim(); + if (!classifier.isEmpty() && !classifier.startsWith("-")) { + classifier = "-" + classifier; + } + //noinspection RedundantIfStatement + if (!project.outputPath().toFile().exists()) { + assert project.outputPath().toFile().mkdirs(); + } + return project.outputPath().resolve(plugFileNameFor(project, classifier)); + } + + private static String plugFileNameFor(Project project, String classifier) { + return project.finalName() + + classifier + + "." + + project.mainArtifact().type().extension(); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java new file mode 100644 index 0000000..0d53e16 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import org.apache.maven.project.MavenProject; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; + +import java.io.File; + +/** + * Creates a packager configuration based on a Maven parameters + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface PackagerConfigurationFactory { + /** + * Creates a configuration + * + * @param project a maven project + * @param logger a logger + * @param classifier classifier of plug to create + * @param attach should created plug be attached to reactor + * @param outputDirectory an output directory + * @param finalName a final name of the source artifact + * @return a configuration + */ + PackagerConfiguration create( + MavenProject project, + Logger logger, + String classifier, + boolean attach, + File outputDirectory, + String finalName + ); +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java new file mode 100644 index 0000000..72dfb9e --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java similarity index 72% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java index 8e9f0fb..611ab1e 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/AbstractFilterableDependency.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java @@ -14,9 +14,12 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.maven.plugin.model; import org.apache.maven.plugins.annotations.Parameter; +import org.apiguardian.api.API; +import org.apiguardian.api.API.Status; +import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; /** * @author Krzysztof Suszynski @@ -24,8 +27,7 @@ * @author David Turanski (Spring Boot project) * @since 0.1.0 */ -abstract class AbstractFilterableDependency - implements FilterableDependency { +abstract class AbstractFilterableDependency implements FilterableDependency { /** * The groupId of the artifact to exclude. @@ -52,31 +54,46 @@ abstract class AbstractFilterableDependency private String classifier; @Override + public String groupId() { + return getGroupId(); + } + + @Override + public String artifactId() { + return getArtifactId(); + } + + @Override + public String classifier() { + return getClassifier(); + } + + @API(status = Status.INTERNAL) public String getGroupId() { return groupId; } - @Override + @API(status = Status.INTERNAL) public void setGroupId(String groupId) { this.groupId = groupId; } - @Override + @API(status = Status.INTERNAL) public String getArtifactId() { return artifactId; } - @Override + @API(status = Status.INTERNAL) public void setArtifactId(String artifactId) { this.artifactId = artifactId; } - @Override + @API(status = Status.INTERNAL) public String getClassifier() { return classifier; } - @Override + @API(status = Status.INTERNAL) public void setClassifier(String classifier) { this.classifier = classifier; } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/Exclude.java similarity index 93% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/Exclude.java index 89abbe4..2aefc09 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Exclude.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/Exclude.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.maven.plugin.model; /** * @author Krzysztof Suszynski diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Include.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/Include.java similarity index 93% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Include.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/Include.java index d45eeb4..2ef598e 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Include.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/Include.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.maven.plugin.model; /** * @author Krzysztof Suszynski diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java similarity index 62% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java index 10c996c..8e2468e 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/ArtifactsLibraries.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java @@ -14,19 +14,24 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.maven.plugin.model; +import io.vavr.collection.HashMap; +import io.vavr.collection.HashSet; +import io.vavr.collection.Map; +import io.vavr.collection.Set; +import io.vavr.collection.Traversable; +import io.vavr.control.Option; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Dependency; -import org.apache.maven.plugin.logging.Log; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.LibraryCallback; +import pl.wavesoftware.plugs.tools.packager.core.model.LibraryScope; +import javax.annotation.Nullable; import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; /** * {@link Libraries} backed by Maven {@link Artifact}s. @@ -37,50 +42,47 @@ * @author Stephane Nicoll (Spring Boot project) * @since 0.1.0 */ -public final class ArtifactsLibraries implements Libraries { - private static final Map SCOPES; - - static { - Map libraryScopes = new HashMap<>(); - libraryScopes.put(Artifact.SCOPE_COMPILE, LibraryScope.COMPILE); - libraryScopes.put(Artifact.SCOPE_RUNTIME, LibraryScope.RUNTIME); - libraryScopes.put(Artifact.SCOPE_PROVIDED, LibraryScope.PROVIDED); - libraryScopes.put(Artifact.SCOPE_SYSTEM, LibraryScope.PROVIDED); - SCOPES = Collections.unmodifiableMap(libraryScopes); - } +public final class MavenLibraries implements Libraries { + private static final Map SCOPES = + HashMap.empty() + .put(Artifact.SCOPE_COMPILE, LibraryScope.COMPILE) + .put(Artifact.SCOPE_RUNTIME, LibraryScope.RUNTIME) + .put(Artifact.SCOPE_PROVIDED, LibraryScope.PROVIDED) + .put(Artifact.SCOPE_SYSTEM, LibraryScope.PROVIDED); private final Set artifacts; - private final Collection unpacks; - private final Log log; + @Nullable + private final Traversable unpacks; + private final Logger logger; - public ArtifactsLibraries( + public MavenLibraries( Set artifacts, - Collection unpacks, - Log log + @Nullable Traversable unpacks, + Logger logger ) { - this.artifacts = Collections.unmodifiableSet(artifacts); - this.unpacks = Collections.unmodifiableCollection(unpacks); - this.log = log; + this.artifacts = artifacts; + this.unpacks = unpacks; + this.logger = logger; } @Override public void doWithLibraries(LibraryCallback callback) throws IOException { Set duplicates = getDuplicates(this.artifacts); for (Artifact artifact : this.artifacts) { - LibraryScope scope = SCOPES.get(artifact.getScope()); - if (scope != null && artifact.getFile() != null) { + Option scopeOption = SCOPES.get(artifact.getScope()); + if (scopeOption.isDefined() && artifact.getFile() != null) { String fileName = getFileName(artifact); StringBuilder name = new StringBuilder(fileName); if (duplicates.contains(fileName)) { - this.log.debug("Duplicate found: " + name); + logger.debug("Duplicate found: {}", name); name.delete(0, name.length()); name.append(artifact.getGroupId()).append("-").append(name); - this.log.debug("Renamed to: " + name); + logger.debug("Renamed to: {}", name); } callback.library(new Library( name.toString(), artifact.getFile(), - scope, + scopeOption.get(), isUnpackRequired(artifact) )); } @@ -100,15 +102,15 @@ private boolean isUnpackRequired(Artifact artifact) { } private static Set getDuplicates(Set artifacts) { - Set duplicates = new HashSet<>(); - Set seen = new HashSet<>(); + java.util.Set duplicates = new java.util.HashSet<>(); + java.util.Set seen = new java.util.HashSet<>(); for (Artifact artifact : artifacts) { String fileName = getFileName(artifact); if (artifact.getFile() != null && !seen.add(fileName)) { duplicates.add(fileName); } } - return duplicates; + return HashSet.ofAll(duplicates); } private static String getFileName(Artifact artifact) { diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java similarity index 93% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java index f5db80d..9a187d2 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/package-info.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java @@ -19,6 +19,6 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.maven.plugin.model; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/package-info.java similarity index 94% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java rename to tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/package-info.java index 0614c5f..b1b30ba 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/package-info.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/package-info.java @@ -19,6 +19,6 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.plugs.tools.maven.plugin; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java similarity index 76% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java index 3c29538..00d0cfb 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/DefaultMojoConfigurator.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecution; @@ -22,24 +22,35 @@ import org.apache.maven.project.MavenProject; import java.nio.file.Path; +import java.util.Optional; +import java.util.function.Supplier; /** * @author Krzysztof Suszynski * @since 0.1.0 */ final class DefaultMojoConfigurator implements MojoConfigurator { + + private final Supplier> customizer; + + DefaultMojoConfigurator(Supplier> customizer) { + this.customizer = customizer; + } + @Override public MavenSession getMavenSession(MojoRule rule, Path pomDirectory) throws Exception { // setup with pom MavenProject project = rule.readMavenProject(pomDirectory.toFile()); + customizer.get().ifPresent(customizer -> customizer.customize(project)); + // Generate session return rule.newMavenSession(project); } @Override public MojoExecution getMojoExecution(MojoRule rule, String goal) { - // Generate ExecutionConfiguration and Mojo for testing + // Generate PackagerConfiguration and Mojo for testing return rule.newMojoExecution(goal); } } diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvoker.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvoker.java new file mode 100644 index 0000000..edb52d3 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvoker.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.maven.testing.junit5; + + +import java.nio.file.Path; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface MavenInvoker { + MavenInvokerExecutor forDirectory(Path pomDirectory); + + interface MavenInvokerExecutor { + void execute(String... goals); + } +} diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvokerExtension.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvokerExtension.java new file mode 100644 index 0000000..007b7ae --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenInvokerExtension.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.maven.testing.junit5; + +import org.apache.maven.cli.MavenCli; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public final class MavenInvokerExtension implements ParameterResolver { + + @Override + public boolean supportsParameter( + ParameterContext parameterContext, + ExtensionContext extensionContext + ) throws ParameterResolutionException { + return MavenInvoker.class.isAssignableFrom( + parameterContext.getParameter().getType() + ); + } + + @Override + public Object resolveParameter( + ParameterContext parameterContext, + ExtensionContext extensionContext + ) throws ParameterResolutionException { + return (MavenInvoker) pomDirectory -> goals -> { + MavenCli cli = new MavenCli(); + System.setProperty( + MavenCli.MULTIMODULE_PROJECT_DIRECTORY, pomDirectory.toString() + ); + int retcode = cli.doMain( + goals, pomDirectory.toString(), System.out, System.err + ); + System.clearProperty(MavenCli.MULTIMODULE_PROJECT_DIRECTORY); + if (retcode != 0) { + throw new IllegalStateException("Maven CLI failed with retcode: " + retcode); + } + }; + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenProjectCustomizer.java similarity index 80% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenProjectCustomizer.java index f4fcea2..430790d 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildFailure.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MavenProjectCustomizer.java @@ -14,12 +14,14 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.maven.testing.junit5; + +import org.apache.maven.project.MavenProject; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface BuildFailure { - String describe(); +public interface MavenProjectCustomizer { + void customize(MavenProject project); } diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilder.java similarity index 96% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilder.java index 6824767..93ab10d 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilder.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilder.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.plugin.AbstractMojo; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderImpl.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java similarity index 98% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderImpl.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java index 560168b..cb157fb 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderImpl.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoConfigurator.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoConfigurator.java similarity index 96% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoConfigurator.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoConfigurator.java index 2390579..71a8771 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoConfigurator.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoConfigurator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecution; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoExtension.java similarity index 86% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoExtension.java index 58a59ea..3adbc1b 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoRuleExtension.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoExtension.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.MojoRule; @@ -32,15 +32,15 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -public final class MojoRuleExtension +public final class MojoExtension implements BeforeAllCallback, ParameterResolver { - private static final Namespace MOJO_NS = Namespace.create(Mojo.class); + private static final Namespace NAMESPACE = Namespace.create(Mojo.class); @Override public void beforeAll(ExtensionContext context) throws Exception { MojoRule rule = new MojoRule(new MojoDelegate()); - context.getStore(MOJO_NS).put(MojoRule.class, rule); + context.getStore(NAMESPACE).put(MojoRule.class, rule); } @Override @@ -51,7 +51,7 @@ public boolean supportsParameter( Parameter parameter = parameterContext.getParameter(); return parameter .getType() - .isAssignableFrom(MojoBuilderFactory.class); + .isAssignableFrom(MojoFactory.class); } @Override @@ -60,9 +60,9 @@ public Object resolveParameter( ExtensionContext extensionContext ) throws ParameterResolutionException { MojoRule mojoRule = extensionContext - .getStore(MOJO_NS) + .getStore(NAMESPACE) .get(MojoRule.class, MojoRule.class); - return new MojoBuilderFactoryImpl(mojoRule); + return new MojoFactoryImpl(mojoRule); } private static final class MojoDelegate extends AbstractMojoTestCase { diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java similarity index 80% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java index c75ebaa..95959c1 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactory.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.plugin.AbstractMojo; @@ -22,7 +22,8 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface MojoBuilderFactory { - MojoBuilderFactory configurator(MojoConfigurator configurator); +public interface MojoFactory { + MojoFactory configurator(MojoConfigurator configurator); + MojoFactory customizer(MavenProjectCustomizer customizer); MojoBuilder builder(Class mojoType); } diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java similarity index 69% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java index a9618e5..6702abc 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/MojoBuilderFactoryImpl.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java @@ -14,31 +14,44 @@ * limitations under the License. */ -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.testing.MojoRule; +import javax.annotation.Nullable; +import java.util.Optional; + /** * @author Krzysztof Suszynski * @since 0.1.0 */ -final class MojoBuilderFactoryImpl implements MojoBuilderFactory { +final class MojoFactoryImpl implements MojoFactory { private final MojoRule rule; - private MojoConfigurator configurator = new DefaultMojoConfigurator(); + @Nullable + private MavenProjectCustomizer customizer; + private MojoConfigurator configurator = new DefaultMojoConfigurator( + () -> Optional.ofNullable(customizer) + ); - MojoBuilderFactoryImpl(MojoRule rule) { + MojoFactoryImpl(MojoRule rule) { this.rule = rule; } @Override - public MojoBuilderFactory configurator(MojoConfigurator configurator) { + public MojoFactory configurator(MojoConfigurator configurator) { this.configurator = configurator; return this; } + @Override + public MojoFactory customizer(MavenProjectCustomizer customizer) { + this.customizer = customizer; + return this; + } + @Override public MojoBuilder builder(Class mojoType) { return new MojoBuilderImpl<>(rule, mojoType, configurator); diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/package-info.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/package-info.java similarity index 94% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/package-info.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/package-info.java index a0442f3..7dacd18 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/junit5/package-info.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/package-info.java @@ -19,6 +19,6 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.maven.junit5; +package pl.wavesoftware.maven.testing.junit5; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java similarity index 51% rename from plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java rename to tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java index cef9079..0ef5a5f 100644 --- a/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/maven/generator/PackagePlugMojoTest.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java @@ -14,29 +14,32 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator; +package pl.wavesoftware.plugs.tools.maven.plugin; +import org.apache.maven.cli.logging.Slf4jLogger; +import org.apache.maven.monitor.logging.DefaultLog; import org.apache.maven.plugin.logging.Log; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; -import pl.wavesoftware.maven.junit5.MojoBuilderFactory; -import pl.wavesoftware.maven.junit5.MojoRuleExtension; +import org.slf4j.LoggerFactory; +import pl.wavesoftware.maven.testing.junit5.MavenInvoker; +import pl.wavesoftware.maven.testing.junit5.MavenInvokerExtension; +import pl.wavesoftware.maven.testing.junit5.MavenProjectCustomizer; +import pl.wavesoftware.maven.testing.junit5.MojoExtension; +import pl.wavesoftware.maven.testing.junit5.MojoFactory; import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Comparator; import static org.assertj.core.api.Assertions.assertThatCode; import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.verify; import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; @@ -46,31 +49,31 @@ * @since 0.1.0 */ @ExtendWith({ - MojoRuleExtension.class, + MojoExtension.class, + MavenInvokerExtension.class, MockitoExtension.class }) -class PackagePlugMojoTest { +class PackagePlugMojoIT { private final Path pomDirectory = Paths.get("simpliest"); + private final MavenProjectCustomizer customizer = project -> { + File destination = fromTargetClasses(pomDirectory) + .resolve("target") + .resolve(project.getBuild().getFinalName() + "." + project.getPackaging()) + .toFile(); + project.getArtifact().setFile(destination); + }; - @Mock - private Log log; + @Spy + private Log log = new DefaultLog(new Slf4jLogger( + LoggerFactory.getLogger(PackagePlugMojoIT.class) + )); @BeforeEach - void before() throws URISyntaxException, IOException { - Path testClasses = - new File(checkNotNull( - getClass().getClassLoader().getResource("."), - "20190117:004601" - ).toURI()).toPath(); - Path target = testClasses.resolve(pomDirectory).resolve("target"); - if (target.toFile().exists()) { - Files.walk(target) - .sorted(Comparator.reverseOrder()) - .forEach(path -> tryToExecute(() -> - Files.deleteIfExists(path), "20190117:005048") - ); - } + void before(MavenInvoker invoker) { + invoker + .forDirectory(fromTargetClasses(pomDirectory)) + .execute("clean", "compile", "jar:jar"); } @AfterEach @@ -80,9 +83,10 @@ void after() { } @Test - void execute(MojoBuilderFactory factory) { + void execute(MojoFactory factory) { // given PackagePlugMojo mojo = factory + .customizer(customizer) .builder(PackagePlugMojo.class) .withPomDirectory(pomDirectory) .build(PackagePlugMojo.GOAL); @@ -90,7 +94,23 @@ void execute(MojoBuilderFactory factory) { // when & then assertThatCode(mojo::execute).doesNotThrowAnyException(); - verify(log).info(contains("simpliest/target/simpliest-0.1.0-plug.jar was successful.")); - verify(log).info(contains("Artifact attached to the build")); + verify(log, atLeastOnce()).isDebugEnabled(); + verify(log).isInfoEnabled(); + verify(log).info(contains("Building plug: ")); + verify(log).debug(contains( + "simpliest-0.1.0-plug.jar was successful." + )); + verify(log).debug(contains("Artifact attached to the build")); + } + + private static Path fromTargetClasses(Path pomDirectory) { + Path testClasses = tryToExecute(() -> + new File(checkNotNull( + PackagePlugMojoIT.class.getClassLoader().getResource("."), + "20190117:004601" + ).toURI()).toPath(), + "20190201:003826" + ); + return testClasses.resolve(pomDirectory); } } diff --git a/plugs-maven-plugin/src/test/resources/simpliest/pom.xml b/tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml similarity index 88% rename from plugs-maven-plugin/src/test/resources/simpliest/pom.xml rename to tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml index 1ae88c9..90cd407 100644 --- a/plugs-maven-plugin/src/test/resources/simpliest/pom.xml +++ b/tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml @@ -25,6 +25,10 @@ pl.wavesoftware.plugs plugs-maven-plugin + @version@ + + xxxx + diff --git a/tools/plugs-packager-core/pom.xml b/tools/plugs-packager-core/pom.xml new file mode 100644 index 0000000..07c1ae6 --- /dev/null +++ b/tools/plugs-packager-core/pom.xml @@ -0,0 +1,60 @@ + + + + + 4.0.0 + + plugs-tools-parent + pl.wavesoftware.plugs.tools + 0.1.0-SNAPSHOT + + + plugs-packager-core + Plugs :: Tools :: Packager Core + + + + pl.wavesoftware + eid-exceptions + + + org.slf4j + slf4j-api + + + javax.inject + javax.inject + + + io.vavr + vavr + + + com.github.zafarkhaja + java-semver + + + org.apache.commons + commons-compress + + + + diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java new file mode 100644 index 0000000..648df57 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core; + +/** + * A packager core constants + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public final class Constants { + public static final String PLUGS_VERSION_ATTRIBUTE = "Plugs-Version"; + public static final String PLUGS_DIGEST_ATTRIBUTE = "Plugs-Digest"; + public static final String LIBRARY_DESTINATION = "PLUGS-INF/lib/"; + + private Constants() { + // non reachable + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java new file mode 100644 index 0000000..02828eb --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core; + +import io.vavr.collection.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.wavesoftware.plugs.tools.packager.core.jar.JarWriter; +import pl.wavesoftware.plugs.tools.packager.core.jar.LibrariesCollector; +import pl.wavesoftware.plugs.tools.packager.core.jar.LibraryHasBeenWritten; +import pl.wavesoftware.plugs.tools.packager.core.jar.WritableLibraries; +import pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilder; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.core.model.Or; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.core.spi.LibrariesFactory; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import static pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed.check; +import static pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed.tring; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class DefaultPackager implements Packager { + + private static final Logger LOGGER = + LoggerFactory.getLogger(DefaultPackager.class); + + private final PackagerConfiguration configuration; + private final RepackagingIsRequired condition; + private final LibrariesFactory librariesFactory; + private final Filter filter; + private final ManifestBuilder manifestBuilder; + + DefaultPackager( + PackagerConfiguration configuration, + RepackagingIsRequired condition, + LibrariesFactory librariesFactory, + Filter filter, + ManifestBuilder manifestBuilder + ) { + this.configuration = configuration; + this.condition = condition; + this.librariesFactory = librariesFactory; + this.filter = filter; + this.manifestBuilder = manifestBuilder; + } + + @Override + public Or repackage() { + return block -> { + if (condition.isTrue()) { + doRepackage(); + } else { + block.execute(); + } + }; + } + + private void doRepackage() throws RepackageFailed { + PackagerCoordinates coordinates = configuration.coordinates(); + Artifact source = validateSource(coordinates.sourceArtifact()); + Path destination = validateDestination(coordinates.targetPath()); + Set artifacts = filter.filterDependencies( + configuration.project().dependencies() + ); + Libraries libraries = librariesFactory.create( + artifacts, configuration.logger() + ); + + try (JarFile sourceJar = newJarFile(source)) { + repackage( + configuration.project(), + sourceJar, + destination.toAbsolutePath(), + libraries + ); + } catch (IOException ex) { + throw new RepackageFailed(ex.getMessage(), ex); + } + } + + private static JarFile newJarFile(Artifact source) throws IOException { + return new JarFile(source.path().toAbsolutePath().toFile()); + } + + private static Path validateDestination(Path destination) + throws RepackageFailed { + File asFile = destination.toFile(); + if (asFile.exists()) { + check(asFile.isFile()).or( + "Invalid destination: {}", + destination + ); + } + return destination; + } + + private static Artifact validateSource(Artifact source) throws RepackageFailed { + check(source.type() == ArtifactType.JAR).or( + "Only jar artifacts are supported at this time" + ); + Path sourcePath = source.path(); + File sourceFile = sourcePath.toFile(); + check(sourceFile.isFile()).or( + "Source must refer to an existing file, got: {}", + sourcePath.toAbsolutePath() + ); + return source; + } + + private void repackage( + Project project, + JarFile sourceJar, + Path destination, + Libraries libraries + ) throws RepackageFailed { + tring(() -> Files.deleteIfExists(destination)).or( + "Can't remove previously created target file: {}", + destination + ); + WritableLibraries writeableLibraries = + tring(() -> new WritableLibraries(libraries)).or( + "Can't read all provided libraries" + ); + LibrariesCollector collector = new LibrariesCollector(); + try (JarWriter writer = newJarWriter(destination)) { + writer.addListener( + LibraryHasBeenWritten.class, + event -> collector.collect(event.getLibrary()) + ); + tring(() -> writeableLibraries.write(writer)).or( + "Can't write libraries to a destination jar, {}", + destination + ); + tring(() -> writer.writeEntries(sourceJar, writeableLibraries)).or( + "Can't rewrite source jar into destination jar: {}", + destination + ); + LOGGER.warn("TODO: Create real imports, from provided scope"); + Manifest manifest = manifestBuilder.buildManifest( + project, + sourceJar + ); + tring(() -> writer.writeManifest(manifest)).or( + "Can't write MANIFEST.MF file int destination jar: {}", + destination + ); + } catch (IOException ex) { + throw new RepackageFailed( + "Can't save destination jar: " + destination, + ex + ); + } + } + + private JarWriter newJarWriter(Path destination) throws RepackageFailed { + return tring(() -> new JarWriter(destination)).or( + "Can't create a jar at destination: {}", + destination + ); + } + +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java new file mode 100644 index 0000000..ced55b5 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core; + +import pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilder; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.core.spi.LibrariesFactory; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class DefaultPackagerFactory implements PackagerFactory { + + private final ManifestBuilder manifestBuilder; + private final RepackagingIsRequiredFactory conditionFactory; + private final LibrariesFactory librariesFactory; + + @Inject + DefaultPackagerFactory( + ManifestBuilder manifestBuilder, + RepackagingIsRequiredFactory conditionFactory, + LibrariesFactory librariesFactory + ) { + this.manifestBuilder = manifestBuilder; + this.conditionFactory = conditionFactory; + this.librariesFactory = librariesFactory; + } + + @Override + public Packager create( + PackagerConfiguration configuration, + Filter filter + ) { + RepackagingIsRequired condition = conditionFactory.create( + configuration.coordinates() + ); + return new DefaultPackager( + configuration, + condition, + librariesFactory, + filter, + manifestBuilder + ); + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java new file mode 100644 index 0000000..366a836 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core; + +import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; +import static pl.wavesoftware.plugs.tools.packager.core.Constants.PLUGS_DIGEST_ATTRIBUTE; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class DefaultRepackagingIsRequired + implements RepackagingIsRequired { + + private final PackagerCoordinates coordinates; + private final FileDigest fileDigest; + + DefaultRepackagingIsRequired( + PackagerCoordinates coordinates, + FileDigest fileDigest + ) { + this.coordinates = coordinates; + this.fileDigest = fileDigest; + } + + @Override + public boolean isTrue() { + return tryToExecute(() -> !alreadyRepackaged(), "20190116:000119"); + } + + private boolean alreadyRepackaged() throws IOException { + File targetFile = coordinates.targetPath().toFile(); + Path sourcePath = coordinates.sourceArtifact().path(); + if (!targetFile.isFile() || !sourcePath.toFile().isFile()) { + return false; + } + try (JarFile jarFile = new JarFile(targetFile)) { + Manifest manifest = jarFile.getManifest(); + CharSequence digest = fileDigest.digest(sourcePath); + return manifest != null + && digest.equals(manifest.getMainAttributes().getValue(PLUGS_DIGEST_ATTRIBUTE)); + } + } + +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java similarity index 56% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java index 6513f61..cea2407 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactoryImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.maven.generator.filter.Filter; -import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; import javax.inject.Inject; import javax.inject.Named; @@ -27,20 +28,19 @@ * @since 0.1.0 */ @Named -final class PackagerFactoryImpl implements PackagerFactory { +final class DefaultRepackagingIsRequiredFactory + implements RepackagingIsRequiredFactory { - private final ManifestBuilder manifestBuilder; + private final FileDigest digest; @Inject - PackagerFactoryImpl(ManifestBuilder manifestBuilder) { - this.manifestBuilder = manifestBuilder; + DefaultRepackagingIsRequiredFactory(FileDigest digest) { + this.digest = digest; } @Override - public Packager create( - ExecutionConfiguration configuration, - Filter filter - ) { - return new PackagerImpl(configuration, filter, manifestBuilder); + public RepackagingIsRequired create(PackagerCoordinates coordinates) { + return new DefaultRepackagingIsRequired(coordinates, digest); } + } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Packager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Packager.java similarity index 58% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Packager.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Packager.java index 86cb337..bed49d8 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/Packager.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Packager.java @@ -14,12 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core; -import org.apache.maven.artifact.Artifact; -import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; - -import java.io.File; +import pl.wavesoftware.plugs.tools.packager.core.model.Or; /** *

@@ -38,31 +35,10 @@ */ public interface Packager { /** - * Checks where repackage action is required - * - * @return true, if repackage is needed - */ - boolean needsRepackaging(); - - /** - * Repackages a artifact as a Plug module - * - * @throws PlugsMojoException if something goes wrong in repackage process - */ - void repackage() throws PlugsMojoException; - - /** - * Gets a source artifact that will be a base for re-packaging as a Plug - * module. - * - * @return a source artifact - */ - Artifact getSourceArtifact(); - - /** - * Gets a target file to be created during packaging process. + * Repackages a artifact as a Plug module, if needed. If not needed + * {@link Or} interface will be used to performs an alternative operation. * - * @return a target file to be repackaged to + * @return the interface that can be used to execute commands if */ - File getTargetFile(); + Or repackage(); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/PackagerFactory.java similarity index 80% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/PackagerFactory.java index 3a6c69c..b20650e 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/PackagerFactory.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/PackagerFactory.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.maven.generator.filter.Filter; -import pl.wavesoftware.plugs.maven.generator.model.ExecutionConfiguration; +import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; /** * A packager factory to produce instances of {@link Packager}. @@ -35,5 +35,5 @@ public interface PackagerFactory { * @param filter a filter for dependencies * @return a created packager instance */ - Packager create(ExecutionConfiguration configuration, Filter filter); + Packager create(PackagerConfiguration configuration, Filter filter); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java new file mode 100644 index 0000000..4c4b5f7 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core; + +import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; + +/** + * A factory of a packaging required condition + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface RepackagingIsRequiredFactory { + /** + * Create a condition that is true if packaging is required. + * + * @param coordinates a coordinates of operation + * @return a condition + */ + RepackagingIsRequired create(PackagerCoordinates coordinates); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java similarity index 95% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java index 6265ba8..adcdb25 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; import javax.annotation.WillClose; import java.io.IOException; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEvent.java similarity index 88% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEvent.java index 8e6eff2..2f32a4b 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterEvent.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEvent.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; /** * An event @@ -22,5 +22,5 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -interface ArchiveWriterEvent { +public interface ArchiveWriterEvent { } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterListener.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterListener.java similarity index 87% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterListener.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterListener.java index df0f240..fba4510 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ArchiveWriterListener.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterListener.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; /** * A listener for specific archive writer event @@ -22,7 +22,7 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -interface ArchiveWriterListener { +public interface ArchiveWriterListener { /** * Handles an raised event diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java similarity index 97% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java index 7f608cb..8e276f3 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/CrcAndSize.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java similarity index 94% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java index 305cb77..79af2db 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryTransformer.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryWriter.java similarity index 95% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryWriter.java index 4c89a0e..22753e6 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/EntryWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryWriter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import java.io.IOException; import java.io.OutputStream; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java new file mode 100644 index 0000000..e1b4331 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import java.io.IOException; +import java.nio.file.Path; + +/** + * A calculator of a digest of a file + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface FileDigest { + /** + * Digest a path to a file + * + * @param path a path to digest + * @return a digest + * @throws IOException if cant be read + */ + CharSequence digest(Path path) throws IOException; +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java new file mode 100644 index 0000000..91e9bf7 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import javax.inject.Named; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.zip.CRC32; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkArgument; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class FileDigestImpl implements FileDigest { + @Override + public CharSequence digest(Path path) throws IOException { + checkArgument(path.toFile().isFile(), "20190131:221929"); + CRC32 digest = new CRC32(); + digest.update(path.toAbsolutePath().toString().getBytes(UTF_8)); + digest.update(Long.toHexString(Files.size(path)).getBytes(UTF_8)); + digest.update(Files.getLastModifiedTime(path).toString().getBytes(UTF_8)); + return Long.toHexString(Math.abs(digest.getValue())); + } +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java similarity index 98% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java index fc21af0..f3d64dd 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/FileUtils.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/IdentityEntryTransformer.java similarity index 94% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/IdentityEntryTransformer.java index c60f6fc..38161d5 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/IdentityEntryTransformer.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/IdentityEntryTransformer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/InputStreamEntryWriter.java similarity index 96% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/InputStreamEntryWriter.java index d557b51..c2a0f85 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/InputStreamEntryWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/InputStreamEntryWriter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import java.io.IOException; import java.io.InputStream; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java similarity index 91% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java index bf04a05..bd220c2 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/JarWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java @@ -14,17 +14,15 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; import org.apache.commons.compress.archivers.zip.UnixStat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.helpers.MessageFormatter; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; import javax.annotation.Nullable; import javax.annotation.WillClose; @@ -37,8 +35,11 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.Enumeration; import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -54,27 +55,25 @@ * @author Andy Wilkinson (Spring Boot project) * @since 0.1.0 */ -final class JarWriter implements ArchiveWriter, AutoCloseable { +public final class JarWriter implements ArchiveWriter, AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(JarWriter.class); private static final UnpackHandler NEVER_UNPACK = new NeverUnpackHandler(); private final JarArchiveOutputStream jarOutput; private final Set writtenEntries = new HashSet<>(); - private final Multimap< - Class, - ArchiveWriterListener - > listeners = HashMultimap.create(); + private final Map, + Set>> listeners = new LinkedHashMap<>(); /** * Create a new {@link JarWriter} instance. * - * @param file the file to write - * @throws IOException if the file cannot be opened - * @throws FileNotFoundException if the file cannot be found + * @param path the path to write + * @throws IOException if the path cannot be opened + * @throws FileNotFoundException if the path cannot be found */ - JarWriter(File file) throws IOException { - FileOutputStream fileOutputStream = new FileOutputStream(file); + public JarWriter(Path path) throws IOException { + FileOutputStream fileOutputStream = new FileOutputStream(path.toFile()); this.jarOutput = new JarArchiveOutputStream(fileOutputStream); this.jarOutput.setEncoding(StandardCharsets.UTF_8.displayName()); } @@ -153,7 +152,7 @@ public void addListener( Class eventType, ArchiveWriterListener listener ) { - listeners.put(eventType, listener); + listeners.computeIfAbsent(eventType, k -> new HashSet<>()).add(listener); } private static void setUpEntry(JarFile jarFile, JarArchiveEntry entry) diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesCollector.java similarity index 80% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesCollector.java index bc5a259..09e9c72 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibrariesCollector.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesCollector.java @@ -14,21 +14,23 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import io.vavr.collection.HashSet; import io.vavr.collection.Set; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; /** + * A collector of a libraries + * * @author Krzysztof Suszynski * @since 0.1.0 */ -final class LibrariesCollector { +public final class LibrariesCollector { private final java.util.Set libraries = new java.util.HashSet<>(); - void collect(Library library) { + public void collect(Library library) { libraries.add(library); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java similarity index 82% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java index cccb495..68c24e1 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryHasBeenWritten.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -final class LibraryHasBeenWritten implements ArchiveWriterEvent { +public final class LibraryHasBeenWritten implements ArchiveWriterEvent { private final Library library; LibraryHasBeenWritten(Library library) { diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java similarity index 90% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java index 1d8d7bf..d62d669 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/LibraryUnpackHandler.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; import java.io.IOException; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java similarity index 88% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java index abefe6a..3b4b2d7 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/NeverUnpackHandler.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; /** * @author Krzysztof Suszynski @@ -29,7 +29,7 @@ public boolean requiresUnpack(String name) { @Override public String sha256Hash(String name) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("Not supported"); } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java similarity index 94% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java index c2c182e..9f41775 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/UnpackHandler.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import java.io.IOException; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java similarity index 76% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java index b36c5ca..fa818d0 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/WritableLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java @@ -14,27 +14,29 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.maven.generator.model.Libraries; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.core.model.Library; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; -import static pl.wavesoftware.plugs.maven.generator.packager.Constants.LIBRARY_DESTINATION; -import static pl.wavesoftware.plugs.maven.generator.packager.FileUtils.isZip; +import static pl.wavesoftware.plugs.tools.packager.core.Constants.LIBRARY_DESTINATION; +import static pl.wavesoftware.plugs.tools.packager.core.jar.FileUtils.isZip; /** + * A libraries that can be written + * * @author Krzysztof Suszynski * @since 0.1.0 */ -final class WritableLibraries implements UnpackHandler { +public final class WritableLibraries implements UnpackHandler { private final Map libraryEntryNames = new LinkedHashMap<>(); - WritableLibraries(Libraries libraries) throws IOException { + public WritableLibraries(Libraries libraries) throws IOException { libraries.doWithLibraries(library -> { if (isZip(library.getFile())) { Library existing = this.libraryEntryNames.putIfAbsent( @@ -66,11 +68,12 @@ public String sha256Hash(String name) throws IOException { return FileUtils.sha256Hash(library.getFile()); } - void write(JarWriter writer) throws IOException { + public void write(JarWriter writer) throws IOException { for (Map.Entry entry : this.libraryEntryNames.entrySet()) { writer.writeNestedLibrary( entry.getKey().substring(0, entry.getKey().lastIndexOf('/') + 1), - entry.getValue()); + entry.getValue() + ); } } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java similarity index 97% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java index ac82195..319d950 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ZipHeaderPeekInputStream.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.jar; import java.io.ByteArrayInputStream; import java.io.FilterInputStream; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java new file mode 100644 index 0000000..682c163 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.core.jar; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilder.java similarity index 64% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilder.java index 1aa56a6..e174883 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/packager/ManifestBuilder.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilder.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.packager; +package pl.wavesoftware.plugs.tools.packager.core.manifest; -import io.vavr.collection.Set; -import org.apache.maven.artifact.Artifact; -import pl.wavesoftware.plugs.maven.generator.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; +import java.util.jar.JarFile; import java.util.jar.Manifest; /** @@ -28,19 +28,15 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -interface ManifestBuilder { +public interface ManifestBuilder { /** * Builds a OSGi manifest of a Jar based on artifact information * - * @param artifact an artifact - * @param dependencies dependencies of an artifact that ware embedded in Jar - * @param imports a imports to be declared + * @param project a project to which manifest should be created + * @param sourceJar a source artifact jar * @return a OSGi manifest for a Plug + * @throws RepackageFailed if source jar can't be reed */ - Manifest buildManifest( - Artifact artifact, - Set dependencies, - Set imports - ); + Manifest buildManifest(Project project, JarFile sourceJar) throws RepackageFailed; } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java new file mode 100644 index 0000000..ca4c836 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.manifest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; + +import javax.inject.Inject; +import javax.inject.Named; +import java.nio.file.Path; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import static pl.wavesoftware.plugs.tools.packager.core.Constants.PLUGS_DIGEST_ATTRIBUTE; +import static pl.wavesoftware.plugs.tools.packager.core.Constants.PLUGS_VERSION_ATTRIBUTE; +import static pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed.tring; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Named +final class ManifestBuilderImpl implements ManifestBuilder { + + private static final Logger LOGGER = + LoggerFactory.getLogger(ManifestBuilderImpl.class); + + private final FileDigest digest; + + @Inject + ManifestBuilderImpl(FileDigest digest) { + this.digest = digest; + } + + @Override + public Manifest buildManifest( + Project project, + JarFile sourceJar + ) throws RepackageFailed { + Path sourcePath = project.mainArtifact().path(); + Manifest manifest = tring(sourceJar::getManifest).or( + "Can't read MANIFEST.MF file from source jar: {}", + sourcePath + ); + if (manifest == null) { + manifest = new Manifest(); + manifest.getMainAttributes() + .putValue("Manifest-Version", "1.0"); + } + manifest = new Manifest(manifest); + Attributes attributes = manifest.getMainAttributes(); + String plugsVersion = getClass().getPackage().getImplementationVersion(); + CharSequence hash = tring(() -> digest.digest(sourcePath)).or( + "Can't calculate digest from source jar: {}", + sourcePath + ); + + attributes.putValue(PLUGS_VERSION_ATTRIBUTE, plugsVersion); + attributes.putValue(PLUGS_DIGEST_ATTRIBUTE, hash.toString()); + + LOGGER.warn("TODO: Define dependencies"); + LOGGER.warn("TODO: Define imports"); + LOGGER.warn("TODO: Define sources"); + LOGGER.warn("TODO: Define name & version"); + + return manifest; + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java new file mode 100644 index 0000000..937dfe5 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.core.manifest; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java new file mode 100644 index 0000000..8c8da27 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import com.github.zafarkhaja.semver.Version; + +import java.nio.file.Path; + +/** + * Represents a artifact of a project + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface Artifact { + + /** + * A name + * + * @return a name + */ + String name(); + + /** + * A version + * + * @return a version + */ + Version version(); + + /** + * A path to a file that this artifact represents + * + * @return a path to a file of the artifact + */ + Path path(); + + /** + * A type of this artifact + * @return a type of this artifact + */ + ArtifactType type(); +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java new file mode 100644 index 0000000..dffa2c8 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public enum ArtifactType { + JAR("jar"), + WAR("war"), + EAR("ear"), + PAR("par"), + RAR("rar"), + ZIP("zip"); + + private final String extension; + + ArtifactType(String extension) { + this.extension = extension; + } + + public String extension() { + return extension; + } + + public static ArtifactType fromPackging(String packaging) { + for (ArtifactType value : ArtifactType.values()) { + if (value.extension().equals(packaging)) { + return value; + } + } + throw new EidIllegalArgumentException("20190209:000032"); + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java new file mode 100644 index 0000000..6229288 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +/** + * A block of code to be executed + */ +@FunctionalInterface +public +interface CodeBlock { + /** + * A simple execute command + */ + void execute(); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Filter.java similarity index 73% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Filter.java index 7701e88..41309a2 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/Filter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Filter.java @@ -14,12 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.packager.core.model; -import org.apache.maven.artifact.Artifact; -import pl.wavesoftware.plugs.maven.generator.model.PlugsMojoException; - -import java.util.Set; +import io.vavr.collection.Set; /** * Filters dependencies according to the rules of a filter. @@ -28,6 +25,13 @@ * @since 0.1.0 */ public interface Filter { + /** + * Filters given dependencies + * + * @param dependencies a dependencies to be filtered + * @return a filtered dependencies + * @throws RepackageFailed if filtering cant be done + */ Set filterDependencies(Set dependencies) - throws PlugsMojoException; + throws RepackageFailed; } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/FilterableDependency.java similarity index 75% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/FilterableDependency.java index 9f78466..d7f9a5b 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/FilterableDependency.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/FilterableDependency.java @@ -14,17 +14,14 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; /** * @author Krzysztof Suszynski * @since 0.1.0 */ public interface FilterableDependency { - String getGroupId(); - void setGroupId(String groupId); - String getArtifactId(); - void setArtifactId(String artifactId); - String getClassifier(); - void setClassifier(String classifier); + String groupId(); + String artifactId(); + String classifier(); } diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java similarity index 81% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java index de91ae1..bb0feb5 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/BuildResult.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java @@ -14,13 +14,14 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; + +import java.io.IOException; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface BuildResult { - boolean isSuccessful(); - Iterable getFailures(); +public interface IoPossibleBlock { + void execute() throws IOException; } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java new file mode 100644 index 0000000..89e2b68 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import java.io.IOException; + +/** + * A supplier that can throw {@link IOException} + * + * @param a type of this supplier + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface IoPossibleSupplier { + /** + * Gets a {@code T} typed result + * @return a result + * @throws IOException a IO operation error + */ + T get() throws IOException; +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Libraries.java similarity index 95% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Libraries.java index c0434fa..6a84af5 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Libraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Libraries.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; import java.io.IOException; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java similarity index 92% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java index 9d87f83..6bb5327 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/Library.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; +import javax.annotation.Nullable; import java.io.File; /** @@ -60,7 +61,12 @@ public Library(File file, LibraryScope scope, boolean unpackRequired) { * @param scope the scope of the library * @param unpackRequired if the library needs to be unpacked before it can be used */ - public Library(String name, File file, LibraryScope scope, boolean unpackRequired) { + public Library( + @Nullable String name, + File file, + LibraryScope scope, + boolean unpackRequired + ) { this.name = (name != null) ? name : file.getName(); this.file = file; this.scope = scope; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryCallback.java similarity index 95% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryCallback.java index 1a1dee5..bd42d67 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryCallback.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryCallback.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; import java.io.File; import java.io.IOException; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryScope.java similarity index 80% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryScope.java index b174dd9..dca7e2c 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/LibraryScope.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryScope.java @@ -14,14 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; /** * The scope of a library. The common {@link #COMPILE}, {@link #RUNTIME} and - * {@link #PROVIDED} scopes are defined here and supported by the common - * {@link Layouts}. - * - * A custom {@link Layout} can handle additional scopes as required. + * {@link #PROVIDED} scopes are defined here and supported. * * @author Krzysztof Suszynski * @author Phillip Webb (Spring Boot project) @@ -31,8 +28,7 @@ public enum LibraryScope { COMPILE("compile"), RUNTIME("runtime"), - PROVIDED("provided"), - CUSTOM("custom"); + PROVIDED("provided"); private final String name; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java new file mode 100644 index 0000000..1e8666e --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +/** + * This interface represent a block of code that should be execute as a + * alternative to the basic operation. + */ +@FunctionalInterface +public interface Or { + /** + * Consumes a block of code + * + * @param block a block of code + * @throws RepackageFailed if repackaging has failed for a some kind of reason + */ + void or(CodeBlock block) throws RepackageFailed; +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java new file mode 100644 index 0000000..910980e --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import org.slf4j.Logger; + +/** + * Represents a configuration of packager + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface PackagerConfiguration { + /** + * A logger to log messages to + * + * @return a logger + */ + Logger logger(); + + /** + * Should created artifact be attached to the build + * + * @return true, if created artifact be attached to the build + */ + boolean shouldAttach(); + + /** + * The coordinates, like source and target paths, to be used + * + * @return a coordinates + */ + PackagerCoordinates coordinates(); + + /** + * A project representation + * + * @return a project representation + */ + Project project(); +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java new file mode 100644 index 0000000..84e3270 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import java.nio.file.Path; + +/** + * A coordinates for packager operation + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface PackagerCoordinates { + /** + * A source artifact that will be a base for re-packaging as a Plug module. + * + * @return a source artifact + */ + Artifact sourceArtifact(); + + /** + * A target file to be created during packaging process. + * + * @return a target file to be repackaged to + */ + Path targetPath(); +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java new file mode 100644 index 0000000..2b3804b --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import io.vavr.collection.Set; + +import java.nio.file.Path; + +/** + * Represents a project to be build + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface Project { + + /** + * A main artifact of a project + * + * @return a main artifact + */ + Artifact mainArtifact(); + + /** + * A project output path + * + * @return a path to output directory + */ + Path outputPath(); + + /** + * A final name of the artifact + * + * @return a final name of the artifact + */ + String finalName(); + + /** + * A Maven classifier, by default it's {@code "plug"} + * + * @return a classifier + */ + String classifier(); + + /** + * A set of dependencies of project + * + * @return a set of dependencies + */ + Set dependencies(); + + /** + * A set of imports of project + * + * @return a set of imports + */ + Set imports(); +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java new file mode 100644 index 0000000..65b8125 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +import org.slf4j.helpers.MessageFormatter; + +import java.io.IOException; +import java.util.function.Supplier; + +/** + * Thrown if repackage operation has failed + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@SuppressWarnings("squid:S1162") +public final class RepackageFailed extends Exception { + private static final long serialVersionUID = 20190208231616L; + + /** + * Default constructor + * + * @param humanReadable a massage that is safe to be presented + */ + public RepackageFailed(String humanReadable) { + super(humanReadable); + } + + /** + * Default constructor + * + * @param humanReadable a massage that is safe to be presented + * @param cause a cause of exception + */ + public RepackageFailed(String humanReadable, Throwable cause) { + super(humanReadable, cause); + } + + public static OrElse check(boolean condition) { + return politeErrorSupplier -> { + if (!condition) { + throw new RepackageFailed(politeErrorSupplier.get()); + } + return null; + }; + } + + public static OrElse tring(IoPossibleBlock block) { + return humanReadableSupplier -> { + try { + block.execute(); + } catch (IOException ex) { + throw new RepackageFailed(humanReadableSupplier.get(), ex); + } + return null; + }; + } + + public static OrElse tring(IoPossibleSupplier supplier) { + return humanReadableSupplier -> { + try { + return supplier.get(); + } catch (IOException ex) { + throw new RepackageFailed(humanReadableSupplier.get(), ex); + } + }; + } + + public interface OrElse { + T or(Supplier politeErrorSupplier) throws RepackageFailed; + default T or(String politeError) throws RepackageFailed { + return or(() -> politeError); + } + default T or(String politeErrorTemplate, Object... args) throws RepackageFailed { + return or( + () -> MessageFormatter + .arrayFormat(politeErrorTemplate, args) + .getMessage() + ); + } + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java new file mode 100644 index 0000000..92ab312 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.model; + +/** + * A interface that represents an entity that can decide if packaging is needed + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface RepackagingIsRequired { + /** + * Checks where repackage action is required + * + * @return true, if repackage is needed + */ + boolean isTrue(); +} diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/StandardLibraries.java similarity index 94% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/StandardLibraries.java index bb0e578..7634fc4 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/model/StandardLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/StandardLibraries.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.maven.generator.model; +package pl.wavesoftware.plugs.tools.packager.core.model; /** * A class that holds list of standard libraries diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java new file mode 100644 index 0000000..af7274b --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.core.model; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java similarity index 93% rename from plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java index 67f15d1..ec2999f 100644 --- a/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/maven/generator/filter/package-info.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java @@ -19,6 +19,6 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.maven.generator.filter; +package pl.wavesoftware.plugs.tools.packager.core; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java new file mode 100644 index 0000000..18f990c --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.spi; + +import io.vavr.collection.Set; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; + + +/** + * A factory for {@link Libraries}. + * + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface LibrariesFactory { + /** + * Creates a implementation of libraries based on a set of project artifacts + * + * @param artifacts a set of project artifacts + * @param logger a logger to perform logging operations + * @return a libraries instance + */ + Libraries create(Set artifacts, Logger logger); +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java new file mode 100644 index 0000000..7c2ada4 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.core.spi; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named b/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named new file mode 100644 index 0000000..18def04 --- /dev/null +++ b/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named @@ -0,0 +1,4 @@ +pl.wavesoftware.plugs.tools.packager.core.jar.FileDigestImpl +pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilderImpl +pl.wavesoftware.plugs.tools.packager.core.DefaultPackagerFactory +pl.wavesoftware.plugs.tools.packager.core.DefaultRepackagingIsRequiredFactory diff --git a/tools/pom.xml b/tools/pom.xml new file mode 100644 index 0000000..6dd4033 --- /dev/null +++ b/tools/pom.xml @@ -0,0 +1,39 @@ + + + + + 4.0.0 + + plugs-parent + pl.wavesoftware.plugs + 0.1.0-SNAPSHOT + + + pl.wavesoftware.plugs.tools + plugs-tools-parent + pom + Plugs :: Tools Parent + + + plugs-packager-core + plugs-maven-plugin + + From ba37fdd01a57151cab15f386713e0a854fdc68dd Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Fri, 22 Mar 2019 19:06:58 +0100 Subject: [PATCH 07/23] Adding Maven transport to be able to fetch artifacts via HTTP --- boms-parent/maven-plugin-bom/pom.xml | 12 ++++++++++++ tools/plugs-maven-plugin/pom.xml | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/boms-parent/maven-plugin-bom/pom.xml b/boms-parent/maven-plugin-bom/pom.xml index 123fe91..be3859b 100644 --- a/boms-parent/maven-plugin-bom/pom.xml +++ b/boms-parent/maven-plugin-bom/pom.xml @@ -62,6 +62,18 @@ 3.1.0 provided + + org.apache.maven.resolver + maven-resolver-connector-basic + 1.3.3 + provided + + + org.apache.maven.resolver + maven-resolver-transport-http + 1.3.3 + provided + org.apache.maven.plugin-tools maven-plugin-annotations diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index 6bf93e8..cb4ca12 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -85,6 +85,16 @@ maven-common-artifact-filters provided + + org.apache.maven.resolver + maven-resolver-connector-basic + provided + + + org.apache.maven.resolver + maven-resolver-transport-http + provided + pl.wavesoftware From 87f000de511343f5a6bc79d89c0e5fb1d70a5c91 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Fri, 22 Mar 2019 19:22:43 +0100 Subject: [PATCH 08/23] Adding AppVeyor --- appveyor.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..3ce55d7 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,22 @@ +version: '{build}' +os: 'Windows Server 2012' +install: + - ps: | + Add-Type -AssemblyName System.IO.Compression.FileSystem + if (!(Test-Path -Path "C:\maven" )) { + (new-object System.Net.WebClient).DownloadFile( + 'http://www.us.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.zip', + 'C:\maven-bin.zip' + ) + [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven") + } + - cmd: SET PATH=C:\maven\apache-maven-3.6.0\bin;%JAVA_HOME%\bin;%PATH% + - cmd: SET MAVEN_OPTS=-Xmx1g + - cmd: SET JAVA_OPTS=-Xmx1g +build_script: + - mvn -V clean package --batch-mode -DskipTest +test_script: + - mvn -V verify +cache: + - C:\maven\ + - C:\Users\appveyor\.m2 From 81797197e3944bd4de3a8a0d5e8a12cb05ef3f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Sun, 24 Mar 2019 14:37:02 +0100 Subject: [PATCH 09/23] Fails nicely if can't create target directories --- .../plugs/tools/packager/core/DefaultPackager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java index 02828eb..9629ba2 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java @@ -148,6 +148,11 @@ private void repackage( "Can't remove previously created target file: {}", destination ); + Path parentPath = destination.getParent(); + tring(() -> Files.createDirectories(parentPath)).or( + "Can't create directory for destination: {}", + parentPath + ); WritableLibraries writeableLibraries = tring(() -> new WritableLibraries(libraries)).or( "Can't read all provided libraries" From 3643265bd3fa5dcea46ddef457a90cc14a091264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Sun, 24 Mar 2019 16:03:52 +0100 Subject: [PATCH 10/23] Plugs maven plugin IT tests passes --- boms-parent/maven-plugin-bom/pom.xml | 22 ++++++++++++------- tools/plugs-maven-plugin/pom.xml | 17 +++++++++----- .../tools/maven/plugin/PackagePlugMojoIT.java | 2 +- .../src/test/resources/simpliest/pom.xml | 15 +++---------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/boms-parent/maven-plugin-bom/pom.xml b/boms-parent/maven-plugin-bom/pom.xml index be3859b..4d0929f 100644 --- a/boms-parent/maven-plugin-bom/pom.xml +++ b/boms-parent/maven-plugin-bom/pom.xml @@ -63,16 +63,22 @@ provided - org.apache.maven.resolver - maven-resolver-connector-basic - 1.3.3 - provided + org.eclipse.aether + aether-connector-basic + 1.0.2.v20150114 + test - org.apache.maven.resolver - maven-resolver-transport-http - 1.3.3 - provided + org.eclipse.aether + aether-transport-wagon + 1.0.2.v20150114 + test + + + org.apache.maven.wagon + wagon-http + 2.10 + test org.apache.maven.plugin-tools diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index cb4ca12..c131844 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -86,14 +86,19 @@ provided - org.apache.maven.resolver - maven-resolver-connector-basic - provided + org.eclipse.aether + aether-connector-basic + test - org.apache.maven.resolver - maven-resolver-transport-http - provided + org.eclipse.aether + aether-transport-wagon + test + + + org.apache.maven.wagon + wagon-http + test diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java index 0ef5a5f..44235b9 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java @@ -98,7 +98,7 @@ void execute(MojoFactory factory) { verify(log).isInfoEnabled(); verify(log).info(contains("Building plug: ")); verify(log).debug(contains( - "simpliest-0.1.0-plug.jar was successful." + "simpliest-0.1.0-plg.jar was successful." )); verify(log).debug(contains("Artifact attached to the build")); } diff --git a/tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml b/tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml index 90cd407..12976c0 100644 --- a/tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml +++ b/tools/plugs-maven-plugin/src/test/resources/simpliest/pom.xml @@ -20,16 +20,7 @@ simpliest 0.1.0 - - - - pl.wavesoftware.plugs - plugs-maven-plugin - @version@ - - xxxx - - - - + + plg + From be8c902f3dd46c13a274201fb953cf99584db8c8 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Mon, 25 Mar 2019 12:24:52 +0100 Subject: [PATCH 11/23] Configure Jacoco reporting for Maven multi module project --- boms-parent/pom.xml | 4 ++++ examples/pom.xml | 4 ++++ plug-api/pom.xml | 4 ++++ plugs-core/pom.xml | 4 ++++ plugs-felix/pom.xml | 4 ++++ plugs-spring/pom.xml | 4 ++++ pom.xml | 34 +++++++++++++++++++++++++++----- testing/pom.xml | 8 ++++++++ tools/plugs-maven-plugin/pom.xml | 4 ++++ tools/pom.xml | 4 ++++ 10 files changed, 69 insertions(+), 5 deletions(-) diff --git a/boms-parent/pom.xml b/boms-parent/pom.xml index 7bdf30a..da15604 100644 --- a/boms-parent/pom.xml +++ b/boms-parent/pom.xml @@ -32,6 +32,10 @@ Plugs :: BOM Parent + + ${project.basedir}/../.. + + maven-plugin-bom diff --git a/examples/pom.xml b/examples/pom.xml index bcd9f80..1dac395 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -32,4 +32,8 @@ pom Plugs :: Examples Parent + + + ${project.basedir}/../.. + diff --git a/plug-api/pom.xml b/plug-api/pom.xml index 8e6ad79..37c07f6 100644 --- a/plug-api/pom.xml +++ b/plug-api/pom.xml @@ -29,4 +29,8 @@ plug-api Plugs :: Plug API + + + ${project.basedir}/.. + diff --git a/plugs-core/pom.xml b/plugs-core/pom.xml index 8777b8e..c9bcd4f 100644 --- a/plugs-core/pom.xml +++ b/plugs-core/pom.xml @@ -30,6 +30,10 @@ Plugs :: Plugs Core + + ${project.basedir}/.. + + org.apiguardian diff --git a/plugs-felix/pom.xml b/plugs-felix/pom.xml index 374776e..f8333f2 100644 --- a/plugs-felix/pom.xml +++ b/plugs-felix/pom.xml @@ -29,6 +29,10 @@ plugs-felix Plugs :: Felix + + ${project.basedir}/.. + + pl.wavesoftware.plugs diff --git a/plugs-spring/pom.xml b/plugs-spring/pom.xml index f028783..a33bdae 100644 --- a/plugs-spring/pom.xml +++ b/plugs-spring/pom.xml @@ -30,6 +30,10 @@ plugs-spring Plugs :: Spring Module + + ${project.basedir}/.. + + pl.wavesoftware.plugs diff --git a/pom.xml b/pom.xml index 920d384..ea0ccfc 100644 --- a/pom.xml +++ b/pom.xml @@ -53,10 +53,6 @@ - - 3.3 - - plug-api plugs-core @@ -97,10 +93,13 @@ + ${project.basedir} UTF-8 UTF-8 ${project.build.directory}/sonar https://sonar.wavesoftware.pl + ${project.rootdir}/target/jacoco.exec + ${project.rootdir}/target/jacoco-it.exec jacoco 8 ${java.source.version} @@ -216,16 +215,41 @@ jacoco-initialize prepare-agent + + + true + ${sonar.jacoco.dataPath} + + + + jacoco-initialize-it + prepare-agent-integration + + true + ${sonar.jacoco.itDataPath} + jacoco-site - post-integration-test + test report + + + ${sonar.jacoco.dataPath} + + + + jacoco-site-it + post-integration-test + report-integration + + ${sonar.jacoco.itDataPath} + diff --git a/testing/pom.xml b/testing/pom.xml index f6d1e62..849243a 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -29,6 +29,10 @@ testing Plugs :: Testing + + ${project.basedir}/.. + + org.springframework.boot @@ -60,6 +64,10 @@ org.junit.jupiter junit-jupiter-engine + + org.junit.vintage + junit-vintage-engine + org.junit.jupiter junit-jupiter-params diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index c131844..1dc1547 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -31,6 +31,10 @@ maven-plugin Plugs :: Tools :: Plugs Maven Plugin + + 3.3 + + diff --git a/tools/pom.xml b/tools/pom.xml index 6dd4033..41313e4 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -32,6 +32,10 @@ pom Plugs :: Tools Parent + + ${project.basedir}/../.. + + plugs-packager-core plugs-maven-plugin From 46084d9dcc90a3fa1791fb23c5496238f4bba701 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Mon, 25 Mar 2019 13:05:23 +0100 Subject: [PATCH 12/23] Configure Jacoco for multimodule project in SonarQube --- pom.xml | 1 + tools/plugs-maven-plugin/pom.xml | 35 ++-- .../tools/maven/plugin/BaseMavenTestCase.java | 76 ++++++++ .../tools/maven/plugin/PackagePlugMojoIT.java | 184 ++++++++++++------ .../src/test/resources/code-with-deps/pom.xml | 37 ++++ .../example/code/HelloService.java | 14 ++ .../src/test/resources/pom/pom.xml | 23 +++ .../src/test/resources/skipped/pom.xml | 26 +++ 8 files changed, 319 insertions(+), 77 deletions(-) create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/BaseMavenTestCase.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/pom/pom.xml create mode 100644 tools/plugs-maven-plugin/src/test/resources/skipped/pom.xml diff --git a/pom.xml b/pom.xml index ea0ccfc..324a463 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,7 @@ https://sonar.wavesoftware.pl ${project.rootdir}/target/jacoco.exec ${project.rootdir}/target/jacoco-it.exec + ${project.rootdir}/target/jacoco.exec,${project.rootdir}/target/jacoco-it.exec jacoco 8 ${java.source.version} diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index 1dc1547..a0c995b 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -89,21 +89,6 @@ maven-common-artifact-filters provided - - org.eclipse.aether - aether-connector-basic - test - - - org.eclipse.aether - aether-transport-wagon - test - - - org.apache.maven.wagon - wagon-http - test - pl.wavesoftware @@ -151,6 +136,26 @@ maven-embedder test + + org.eclipse.aether + aether-connector-basic + test + + + org.eclipse.aether + aether-transport-wagon + test + + + org.apache.maven.wagon + wagon-http + test + + + org.springframework + spring-core + test + diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/BaseMavenTestCase.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/BaseMavenTestCase.java new file mode 100644 index 0000000..d92074e --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/BaseMavenTestCase.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import pl.wavesoftware.maven.testing.junit5.MavenInvoker; +import pl.wavesoftware.maven.testing.junit5.MavenInvokerExtension; +import pl.wavesoftware.maven.testing.junit5.MavenProjectCustomizer; +import pl.wavesoftware.maven.testing.junit5.MojoExtension; + +import java.io.File; +import java.nio.file.Path; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; + +@ExtendWith({ + MojoExtension.class, + MavenInvokerExtension.class +}) +abstract class BaseMavenTestCase { + private final Path pomDirectory; + private final MavenProjectCustomizer customizer; + + BaseMavenTestCase(Path pomDirectory) { + this.pomDirectory = pomDirectory; + customizer = project -> { + File destination = fromTargetClasses(pomDirectory) + .resolve("target") + .resolve(project.getBuild().getFinalName() + "." + project.getPackaging()) + .toFile(); + project.getArtifact().setFile(destination); + }; + } + + @BeforeEach + void before(MavenInvoker invoker) { + invoker + .forDirectory(fromTargetClasses(pomDirectory)) + .execute("clean", "package"); + } + + MavenProjectCustomizer getCustomizer() { + return customizer; + } + + Path getPomDirectory() { + return pomDirectory; + } + + private static Path fromTargetClasses(Path pomDirectory) { + Path testClasses = tryToExecute(() -> + new File(checkNotNull( + PackagePlugMojoIT.class.getClassLoader().getResource("."), + "20190117:004601" + ).toURI()).toPath(), + "20190201:003826" + ); + return testClasses.resolve(pomDirectory); + } +} diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java index 44235b9..d93ba7f 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java @@ -20,97 +20,157 @@ import org.apache.maven.monitor.logging.DefaultLog; import org.apache.maven.plugin.logging.Log; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.LoggerFactory; -import pl.wavesoftware.maven.testing.junit5.MavenInvoker; -import pl.wavesoftware.maven.testing.junit5.MavenInvokerExtension; -import pl.wavesoftware.maven.testing.junit5.MavenProjectCustomizer; -import pl.wavesoftware.maven.testing.junit5.MojoExtension; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; import pl.wavesoftware.maven.testing.junit5.MojoFactory; -import java.io.File; -import java.nio.file.Path; import java.nio.file.Paths; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.mockito.ArgumentMatchers.contains; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.verify; -import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; -import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; /** * @author Krzysztof Suszynski * @since 0.1.0 */ -@ExtendWith({ - MojoExtension.class, - MavenInvokerExtension.class, - MockitoExtension.class -}) class PackagePlugMojoIT { - private final Path pomDirectory = Paths.get("simpliest"); - private final MavenProjectCustomizer customizer = project -> { - File destination = fromTargetClasses(pomDirectory) - .resolve("target") - .resolve(project.getBuild().getFinalName() + "." + project.getPackaging()) - .toFile(); - project.getArtifact().setFile(destination); - }; - - @Spy - private Log log = new DefaultLog(new Slf4jLogger( + private static final Log DEFAULT_LOG = new DefaultLog(new Slf4jLogger( LoggerFactory.getLogger(PackagePlugMojoIT.class) )); - @BeforeEach - void before(MavenInvoker invoker) { - invoker - .forDirectory(fromTargetClasses(pomDirectory)) - .execute("clean", "compile", "jar:jar"); + @Nested + @ExtendWith(MockitoExtension.class) + class Simpliest extends BaseMavenTestCase { + @Spy + private Log log = DEFAULT_LOG; + + Simpliest() { + super(Paths.get("simpliest")); + } + + @AfterEach + void after() { + Mockito.verifyNoMoreInteractions(log); + Mockito.validateMockitoUsage(); + } + + @Test + @DisplayName("Execute mojo on simpliest project") + void execute(MojoFactory factory) { + // given + PackagePlugMojo mojo = factory + .customizer(getCustomizer()) + .builder(PackagePlugMojo.class) + .withPomDirectory(getPomDirectory()) + .build(PackagePlugMojo.GOAL); + mojo.setLog(log); + + // when & then + assertThatCode(mojo::execute).doesNotThrowAnyException(); + verify(log, atLeastOnce()).isDebugEnabled(); + verify(log).isInfoEnabled(); + verify(log).info(contains("Building plug: ")); + verify(log).debug(contains( + "simpliest-0.1.0-plg.jar was successful." + )); + verify(log).debug(contains("Artifact attached to the build")); + Resource resource = new ClassPathResource( + "/simpliest/target/simpliest-0.1.0-plg.jar" + ); + assertThat(resource.exists()).isTrue(); + } } - @AfterEach - void after() { - Mockito.verifyNoMoreInteractions(log); - Mockito.validateMockitoUsage(); + @Nested + class Pom extends BaseMavenTestCase { + + Pom() { + super(Paths.get("pom")); + } + + @Test + @DisplayName("Execute mojo on pom project") + void execute(MojoFactory factory) { + // given + PackagePlugMojo mojo = factory + .customizer(getCustomizer()) + .builder(PackagePlugMojo.class) + .withPomDirectory(getPomDirectory()) + .build(PackagePlugMojo.GOAL); + mojo.setLog(DEFAULT_LOG); + + // when & then + assertThatCode(mojo::execute).doesNotThrowAnyException(); + Resource resource = new ClassPathResource( + "/pom/target/a-pom-0.1.0-plug.jar" + ); + assertThat(resource.exists()).isFalse(); + } } - @Test - void execute(MojoFactory factory) { - // given - PackagePlugMojo mojo = factory - .customizer(customizer) - .builder(PackagePlugMojo.class) - .withPomDirectory(pomDirectory) - .build(PackagePlugMojo.GOAL); - mojo.setLog(log); - - // when & then - assertThatCode(mojo::execute).doesNotThrowAnyException(); - verify(log, atLeastOnce()).isDebugEnabled(); - verify(log).isInfoEnabled(); - verify(log).info(contains("Building plug: ")); - verify(log).debug(contains( - "simpliest-0.1.0-plg.jar was successful." - )); - verify(log).debug(contains("Artifact attached to the build")); + @Nested + class Skipped extends BaseMavenTestCase { + + Skipped() { + super(Paths.get("skipped")); + } + + @Test + @DisplayName("Execute mojo on skipped project") + void execute(MojoFactory factory) { + // given + PackagePlugMojo mojo = factory + .customizer(getCustomizer()) + .builder(PackagePlugMojo.class) + .withPomDirectory(getPomDirectory()) + .build(PackagePlugMojo.GOAL); + mojo.setLog(DEFAULT_LOG); + + // when & then + assertThatCode(mojo::execute).doesNotThrowAnyException(); + Resource resource = new ClassPathResource( + "/skipped/target/skipped-0.1.0-plug.jar" + ); + assertThat(resource.exists()).isFalse(); + } } - private static Path fromTargetClasses(Path pomDirectory) { - Path testClasses = tryToExecute(() -> - new File(checkNotNull( - PackagePlugMojoIT.class.getClassLoader().getResource("."), - "20190117:004601" - ).toURI()).toPath(), - "20190201:003826" - ); - return testClasses.resolve(pomDirectory); + @Nested + class CodeWithDependencies extends BaseMavenTestCase { + CodeWithDependencies() { + super(Paths.get("code-with-deps")); + } + + @Test + @DisplayName("Execute mojo on code-with-deps project") + void execute(MojoFactory factory) { + // given + PackagePlugMojo mojo = factory + .customizer(getCustomizer()) + .builder(PackagePlugMojo.class) + .withPomDirectory(getPomDirectory()) + .build(PackagePlugMojo.GOAL); + mojo.setLog(DEFAULT_LOG); + + // when & then + assertThatCode(mojo::execute).doesNotThrowAnyException(); + Resource resource = new ClassPathResource( + "/code-with-deps/target/code-with-deps-0.1.0-plug.jar" + ); + assertThat(resource.exists()).isTrue(); + } } + } diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml new file mode 100644 index 0000000..91a85a8 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml @@ -0,0 +1,37 @@ + + + + 4.0.0 + org.example + code-with-deps + 0.1.0 + + + + org.springframework + spring-context + 5.1.5.RELEASE + provided + + + + org.apiguardian + apiguardian-api + 1.0.0 + + + diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java new file mode 100644 index 0000000..babf1b5 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java @@ -0,0 +1,14 @@ +package pl.wavesoftware.example.code; + +import org.apiguardian.api.API; +import org.springframework.stereotype.Service; + +import static org.apiguardian.api.API.Status.STABLE; + +@Service +@API(status = STABLE) +public class HelloService { + String hello() { + return "Hello from Plug"; + } +} diff --git a/tools/plugs-maven-plugin/src/test/resources/pom/pom.xml b/tools/plugs-maven-plugin/src/test/resources/pom/pom.xml new file mode 100644 index 0000000..04812b9 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/pom/pom.xml @@ -0,0 +1,23 @@ + + + + 4.0.0 + org.example + a-pom + 0.1.0 + pom + diff --git a/tools/plugs-maven-plugin/src/test/resources/skipped/pom.xml b/tools/plugs-maven-plugin/src/test/resources/skipped/pom.xml new file mode 100644 index 0000000..d2db80e --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/skipped/pom.xml @@ -0,0 +1,26 @@ + + + + 4.0.0 + org.example + skipped + 0.1.0 + + + true + + From a1815083766bf34349759c9a3ff0d917ed7491a7 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Mon, 25 Mar 2019 21:06:08 +0100 Subject: [PATCH 13/23] Working IT cases with proper manifest basic entries --- plug-api/pom.xml | 21 ++++++ .../wavesoftware/plugs/api/PlugsVersion.java | 73 +++++++++++++++++++ .../plugs/api/PlugsVersionIT.java | 41 +++++++++++ plugs-core/pom.xml | 1 + plugs-felix/pom.xml | 1 + plugs-spring/pom.xml | 1 + pom.xml | 43 +++++++++++ testing/pom.xml | 1 + tools/plugs-maven-plugin/pom.xml | 4 + tools/plugs-packager-core/pom.xml | 10 +++ .../packager/core/jar/EntryTransformer.java | 2 + .../packager/core/jar/FileDigestImpl.java | 1 - .../tools/packager/core/jar/JarWriter.java | 13 +++- .../core/jar/SkipManifestMfTransformer.java | 35 +++++++++ .../core/manifest/ManifestBuilderImpl.java | 3 +- 15 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java create mode 100644 plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/SkipManifestMfTransformer.java diff --git a/plug-api/pom.xml b/plug-api/pom.xml index 37c07f6..fa0dbfa 100644 --- a/plug-api/pom.xml +++ b/plug-api/pom.xml @@ -32,5 +32,26 @@ ${project.basedir}/.. + false + + + + pl.wavesoftware + eid-exceptions + + + + + pl.wavesoftware.plugs + testing + ${project.version} + test + + + com.github.zafarkhaja + java-semver + test + + diff --git a/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java b/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java new file mode 100644 index 0000000..96518ca --- /dev/null +++ b/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.api; + +import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException; + +import java.io.InputStream; +import java.net.URL; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.jar.Manifest; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkState; + +public final class PlugsVersion { + + private PlugsVersion() { + // not reachable + } + + public static String getVersion() { + Optional maybeVersion = + Optional.ofNullable( + PlugsVersion.class.getPackage().getImplementationVersion() + ); + return checkNotNull( + maybeVersion.orElseGet(PlugsVersion::manuallyRead), + "20190325:202509" + ); + } + + static String manuallyRead() { + List urls = Collections.list(tryToExecute( + () -> PlugsVersion.class + .getClassLoader() + .getResources("META-INF/MANIFEST.MF"), + "20190325:205203" + )); + checkState(!urls.isEmpty(), "20190325:205809"); + URL location = PlugsVersion.class.getProtectionDomain().getCodeSource().getLocation(); + URL resource = urls.stream() + .filter(url -> url.toString().contains(location.toString())) + .findFirst() + .orElseThrow(() -> new EidIllegalArgumentException("20190325:205648")); + InputStream inputStream = checkNotNull( + tryToExecute(resource::openStream, "20190325:205720"), + "20190325:204807" + ); + Manifest manifest = tryToExecute( + () -> new Manifest(inputStream), + "20190325:204433" + ); + return manifest.getMainAttributes() + .getValue("Implementation-Version"); + } +} diff --git a/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java new file mode 100644 index 0000000..6efacd1 --- /dev/null +++ b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.api; + +import com.github.zafarkhaja.semver.Version; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlugsVersionIT { + + private static final Version BASE = Version.forIntegers(0, 0, 0); + + @Test + void getVersion() { + Version version = Version.valueOf(PlugsVersion.getVersion()); + + assertThat(version.greaterThanOrEqualTo(BASE)).isTrue(); + } + + @Test + void manually() { + Version version = Version.valueOf(PlugsVersion.manuallyRead()); + + assertThat(version.greaterThanOrEqualTo(BASE)).isTrue(); + } +} diff --git a/plugs-core/pom.xml b/plugs-core/pom.xml index c9bcd4f..94246a2 100644 --- a/plugs-core/pom.xml +++ b/plugs-core/pom.xml @@ -32,6 +32,7 @@ ${project.basedir}/.. + false diff --git a/plugs-felix/pom.xml b/plugs-felix/pom.xml index f8333f2..9efc251 100644 --- a/plugs-felix/pom.xml +++ b/plugs-felix/pom.xml @@ -31,6 +31,7 @@ ${project.basedir}/.. + false diff --git a/plugs-spring/pom.xml b/plugs-spring/pom.xml index a33bdae..2e33397 100644 --- a/plugs-spring/pom.xml +++ b/plugs-spring/pom.xml @@ -32,6 +32,7 @@ ${project.basedir}/.. + false diff --git a/pom.xml b/pom.xml index 324a463..8ba309b 100644 --- a/pom.xml +++ b/pom.xml @@ -106,6 +106,7 @@ ${java.source.version} 1.${java.source.version} ${maven.compiler.source} + true ${skipTests} @@ -273,6 +274,22 @@ maven-jar-plugin 3.1.1 + + + + true + true + + + ${project.artifactId} + + + + + + + maven-dependency-plugin + 3.1.1 @@ -373,6 +390,32 @@ maven-failsafe-plugin + + + maven-dependency-plugin + + + unpack-manifest-mf + package + + unpack + + + ${unpack-manifestmf.skip} + + + ${project.groupId} + ${project.artifactId} + ${project.version} + ${project.packaging} + META-INF/MANIFEST.MF + + + ${project.build.directory}/classes + + + + diff --git a/testing/pom.xml b/testing/pom.xml index 849243a..d8ebb4a 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -31,6 +31,7 @@ ${project.basedir}/.. + false diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index a0c995b..10859e9 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -35,6 +35,10 @@ 3.3 + + false + + diff --git a/tools/plugs-packager-core/pom.xml b/tools/plugs-packager-core/pom.xml index 07c1ae6..597f686 100644 --- a/tools/plugs-packager-core/pom.xml +++ b/tools/plugs-packager-core/pom.xml @@ -29,8 +29,18 @@ plugs-packager-core Plugs :: Tools :: Packager Core + + + false + + + pl.wavesoftware.plugs + plug-api + ${project.version} + + pl.wavesoftware eid-exceptions diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java index 79af2db..7753df3 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/EntryTransformer.java @@ -18,6 +18,7 @@ import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import javax.annotation.Nullable; import java.util.jar.JarEntry; /** @@ -28,5 +29,6 @@ * @since 0.1.0 */ interface EntryTransformer { + @Nullable JarArchiveEntry transform(JarArchiveEntry jarEntry); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java index 91e9bf7..adab60e 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java @@ -37,7 +37,6 @@ public CharSequence digest(Path path) throws IOException { CRC32 digest = new CRC32(); digest.update(path.toAbsolutePath().toString().getBytes(UTF_8)); digest.update(Long.toHexString(Files.size(path)).getBytes(UTF_8)); - digest.update(Files.getLastModifiedTime(path).toString().getBytes(UTF_8)); return Long.toHexString(Math.abs(digest.getValue())); } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java index bd220c2..37d541c 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java @@ -118,12 +118,12 @@ public void writeNestedLibrary( @Override public void writeEntries(JarFile jarFile) throws IOException { - this.writeEntries(jarFile, new IdentityEntryTransformer(), NEVER_UNPACK); + this.writeEntries(jarFile, new SkipManifestMfTransformer(), NEVER_UNPACK); } @Override public void writeEntries(JarFile jarFile, UnpackHandler unpackHandler) throws IOException { - this.writeEntries(jarFile, new IdentityEntryTransformer(), unpackHandler); + this.writeEntries(jarFile, new SkipManifestMfTransformer(), unpackHandler); } @Override @@ -233,9 +233,11 @@ private void writeEntry( UnpackHandler unpackHandler ) throws IOException { String parent = entry.getName(); + boolean isDirectory = false; if (parent.endsWith("/")) { parent = parent.substring(0, parent.length() - 1); entry.setUnixMode(UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM); + isDirectory = true; } else { entry.setUnixMode(UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM); } @@ -253,6 +255,13 @@ private void writeEntry( entryWriter.write(this.jarOutput); } this.jarOutput.closeArchiveEntry(); + } else { + if (!isDirectory) { + LOGGER.warn( + "Skipping resource, as it was already written: {}", + entry.getName() + ); + } } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/SkipManifestMfTransformer.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/SkipManifestMfTransformer.java new file mode 100644 index 0000000..d8ab6b7 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/SkipManifestMfTransformer.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; + +import javax.annotation.Nullable; + +final class SkipManifestMfTransformer implements EntryTransformer { + + private final EntryTransformer delegate = new IdentityEntryTransformer(); + + @Override + @Nullable + public JarArchiveEntry transform(JarArchiveEntry jarEntry) { + if (jarEntry.getName().equals("META-INF/MANIFEST.MF")) { + return null; + } + return delegate.transform(jarEntry); + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java index ca4c836..e96b762 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java @@ -18,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pl.wavesoftware.plugs.api.PlugsVersion; import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; import pl.wavesoftware.plugs.tools.packager.core.model.Project; import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; @@ -67,7 +68,7 @@ public Manifest buildManifest( } manifest = new Manifest(manifest); Attributes attributes = manifest.getMainAttributes(); - String plugsVersion = getClass().getPackage().getImplementationVersion(); + String plugsVersion = PlugsVersion.getVersion(); CharSequence hash = tring(() -> digest.digest(sourcePath)).or( "Can't calculate digest from source jar: {}", sourcePath From d675ab543c4a7d0ced722248ede1acbd1b30ac64 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Thu, 28 Mar 2019 20:41:23 +0100 Subject: [PATCH 14/23] Removing unpacks, adding code to properly handle Maven dependencies --- .../maven/plugin/filter/MavenFilter.java | 3 +- .../maven/plugin/mapper/ArtifactMapper.java | 2 + .../plugin/mapper/ArtifactMapperImpl.java | 19 ++++++ .../plugin/mapper/MavenBackedProject.java | 13 ++-- .../maven/plugin/mapper/MavenDependency.java | 68 +++++++++++++++++++ .../plugin/mapper/MavenLibrariesFactory.java | 1 - .../maven/plugin/model/MavenLibraries.java | 22 +----- .../tools/packager/core/DefaultPackager.java | 2 +- .../packager/core/jar/ArchiveWriter.java | 8 +-- .../tools/packager/core/jar/FileUtils.java | 43 ------------ .../tools/packager/core/jar/JarWriter.java | 49 ++----------- .../core/jar/LibraryUnpackHandler.java | 45 ------------ .../packager/core/jar/NeverUnpackHandler.java | 35 ---------- .../packager/core/jar/UnpackHandler.java | 32 --------- .../packager/core/jar/WritableLibraries.java | 18 +---- .../tools/packager/core/model/CodeBlock.java | 3 +- .../tools/packager/core/model/Library.java | 26 +------ 17 files changed, 110 insertions(+), 279 deletions(-) create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java delete mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java delete mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java delete mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java index c068643..93ed40c 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java @@ -42,10 +42,9 @@ final class MavenFilter implements Filter { public Set filterDependencies(Set dependencies) throws RepackageFailed { try { - Set delta = HashSet.ofAll( + return HashSet.ofAll( filters.filter(dependencies.map(artifactMapper::mavenize).toJavaSet()) ).map(artifactMapper::generalize); - return dependencies.retainAll(delta); } catch (ArtifactFilterException ex) { throw new RepackageFailed( "Can't filter Maven dependencies using provided filters", diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java index 6ea8416..dd60134 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java @@ -16,6 +16,7 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; +import org.apache.maven.model.Dependency; import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; /** @@ -25,4 +26,5 @@ public interface ArtifactMapper { Artifact generalize(org.apache.maven.artifact.Artifact artifact); org.apache.maven.artifact.Artifact mavenize(Artifact artifact); + Artifact map(Dependency dependency); } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java index 298b5d7..a0e46e5 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java @@ -16,8 +16,11 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; +import org.apache.maven.model.Dependency; +import org.apache.maven.repository.RepositorySystem; import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import javax.inject.Inject; import javax.inject.Named; /** @@ -26,6 +29,14 @@ */ @Named final class ArtifactMapperImpl implements ArtifactMapper { + + private final RepositorySystem repositorySystem; + + @Inject + ArtifactMapperImpl(RepositorySystem repositorySystem) { + this.repositorySystem = repositorySystem; + } + @Override public Artifact generalize(org.apache.maven.artifact.Artifact artifact) { return new MavenArtifact(artifact); @@ -36,8 +47,16 @@ public org.apache.maven.artifact.Artifact mavenize(Artifact artifact) { if (artifact instanceof MavenArtifact) { return ((MavenArtifact) artifact).getDelegate(); } + if (artifact instanceof MavenDependency) { + return ((MavenDependency) artifact).asArtifact(); + } throw new UnsupportedOperationException( "Not supported artifact type: " + artifact.getClass() ); } + + @Override + public Artifact map(Dependency dependency) { + return new MavenDependency(repositorySystem, dependency); + } } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java index 224bbd8..01395ed 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java @@ -19,6 +19,7 @@ import io.vavr.Lazy; import io.vavr.collection.HashSet; import io.vavr.collection.Set; +import org.apache.maven.model.Dependency; import org.apache.maven.project.MavenProject; import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; import pl.wavesoftware.plugs.tools.packager.core.model.Project; @@ -88,19 +89,19 @@ public Set imports() { private Set calculateDependencies() { return HashSet - .ofAll(mavenProject.getArtifacts()) + .ofAll(mavenProject.getDependencies()) .reject(MavenBackedProject::hasProvidedScope) - .map(artifactMapper::generalize); + .map(artifactMapper::map); } private Set calculateImports() { return HashSet - .ofAll(mavenProject.getArtifacts()) + .ofAll(mavenProject.getDependencies()) .filter(MavenBackedProject::hasProvidedScope) - .map(artifactMapper::generalize); + .map(artifactMapper::map); } - private static boolean hasProvidedScope(org.apache.maven.artifact.Artifact artifact) { - return artifact.getScope().equals(org.apache.maven.artifact.Artifact.SCOPE_SYSTEM); + private static boolean hasProvidedScope(Dependency dependency) { + return dependency.getScope().equals(org.apache.maven.artifact.Artifact.SCOPE_PROVIDED); } } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java new file mode 100644 index 0000000..54c3ead --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import com.github.zafarkhaja.semver.Version; +import io.vavr.Lazy; +import org.apache.maven.model.Dependency; +import org.apache.maven.repository.RepositorySystem; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; + +import java.nio.file.Path; + +final class MavenDependency implements Artifact { + private final RepositorySystem repositorySystem; + private final Dependency delegate; + private final Lazy artifactLazy = Lazy.of( + this::mapAsArtifact + ); + + MavenDependency(RepositorySystem repositorySystem, Dependency dependency) { + this.repositorySystem = repositorySystem; + this.delegate = dependency; + } + + @Override + public String name() { + return delegate.getArtifactId(); + } + + @Override + public Version version() { + return Version.valueOf(delegate.getVersion()); + } + + @Override + public Path path() { + org.apache.maven.artifact.Artifact artifact = asArtifact(); + return artifact.getFile().toPath(); + } + + @Override + public ArtifactType type() { + return ArtifactType.fromPackging(delegate.getType()); + } + + org.apache.maven.artifact.Artifact asArtifact() { + return artifactLazy.get(); + } + + private org.apache.maven.artifact.Artifact mapAsArtifact() { + return repositorySystem.createDependencyArtifact(delegate); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java index e1e8a53..c21b20f 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java @@ -44,7 +44,6 @@ final class MavenLibrariesFactory implements LibrariesFactory { public Libraries create(Set artifacts, Logger logger) { return new MavenLibraries( artifacts.map(artifactMapper::mavenize), - null, logger ); } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java index 8e2468e..bc3d97c 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java @@ -20,17 +20,14 @@ import io.vavr.collection.HashSet; import io.vavr.collection.Map; import io.vavr.collection.Set; -import io.vavr.collection.Traversable; import io.vavr.control.Option; import org.apache.maven.artifact.Artifact; -import org.apache.maven.model.Dependency; import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; import pl.wavesoftware.plugs.tools.packager.core.model.Library; import pl.wavesoftware.plugs.tools.packager.core.model.LibraryCallback; import pl.wavesoftware.plugs.tools.packager.core.model.LibraryScope; -import javax.annotation.Nullable; import java.io.IOException; /** @@ -51,17 +48,13 @@ public final class MavenLibraries implements Libraries { .put(Artifact.SCOPE_SYSTEM, LibraryScope.PROVIDED); private final Set artifacts; - @Nullable - private final Traversable unpacks; private final Logger logger; public MavenLibraries( Set artifacts, - @Nullable Traversable unpacks, Logger logger ) { this.artifacts = artifacts; - this.unpacks = unpacks; this.logger = logger; } @@ -82,25 +75,12 @@ public void doWithLibraries(LibraryCallback callback) throws IOException { callback.library(new Library( name.toString(), artifact.getFile(), - scopeOption.get(), - isUnpackRequired(artifact) + scopeOption.get() )); } } } - private boolean isUnpackRequired(Artifact artifact) { - if (this.unpacks != null) { - for (Dependency unpack : this.unpacks) { - if (artifact.getGroupId().equals(unpack.getGroupId()) - && artifact.getArtifactId().equals(unpack.getArtifactId())) { - return true; - } - } - } - return false; - } - private static Set getDuplicates(Set artifacts) { java.util.Set duplicates = new java.util.HashSet<>(); java.util.Set seen = new java.util.HashSet<>(); diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java index 9629ba2..1d580ac 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java @@ -167,7 +167,7 @@ private void repackage( "Can't write libraries to a destination jar, {}", destination ); - tring(() -> writer.writeEntries(sourceJar, writeableLibraries)).or( + tring(() -> writer.writeEntries(sourceJar)).or( "Can't rewrite source jar into destination jar: {}", destination ); diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java index adcdb25..22c0444 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java @@ -73,13 +73,7 @@ void writeNestedLibrary( void writeEntries( JarFile jarFile, - UnpackHandler unpackHandler - ) throws IOException; - - void writeEntries( - JarFile jarFile, - EntryTransformer entryTransformer, - UnpackHandler unpackHandler + EntryTransformer entryTransformer ) throws IOException; void addListener( diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java index f3d64dd..b92c19a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java @@ -19,16 +19,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.WillNotClose; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.security.DigestInputStream; -import java.security.MessageDigest; - -import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; /** * Utilities for manipulating files and directories in Spring Boot tooling. @@ -41,7 +35,6 @@ final class FileUtils { private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class); - private static final int BUFFER_4K = 4098; private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 }; private FileUtils() { @@ -70,40 +63,4 @@ private static boolean isZip(InputStream inputStream) throws IOException { return true; } - /** - * Generate a SHA.1 Hash for a given file. - * @param file the file to hash - * @return the hash value as a String - * @throws IOException if the file cannot be read - */ - static String sha256Hash(File file) throws IOException { - MessageDigest digest = tryToExecute( - () -> MessageDigest.getInstance("SHA-256"), - "20190115:225947" - ); - try (DigestInputStream inputStream = newDigestInputStream(file, digest)) { - byte[] buffer = new byte[BUFFER_4K]; - //noinspection StatementWithEmptyBody - while (inputStream.read(buffer) != -1) { - // Read the entire stream - } - return bytesToHex(inputStream.getMessageDigest().digest()); - } - } - - @WillNotClose - private static DigestInputStream newDigestInputStream( - File file, - MessageDigest digest - ) throws FileNotFoundException { - return new DigestInputStream(new FileInputStream(file), digest); - } - - private static String bytesToHex(byte[] bytes) { - StringBuilder hex = new StringBuilder(); - for (byte b : bytes) { - hex.append(String.format("%02x", b)); - } - return hex.toString(); - } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java index 37d541c..bed7483 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java @@ -26,8 +26,6 @@ import javax.annotation.Nullable; import javax.annotation.WillClose; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -58,7 +56,6 @@ public final class JarWriter implements ArchiveWriter, AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(JarWriter.class); - private static final UnpackHandler NEVER_UNPACK = new NeverUnpackHandler(); private final JarArchiveOutputStream jarOutput; private final Set writtenEntries = new HashSet<>(); @@ -104,8 +101,7 @@ public void writeNestedLibrary( new CrcAndSize(file).setupStoredEntry(entry); writeEntry( entry, - new InputStreamEntryWriter(new FileInputStream(file), true), - new LibraryUnpackHandler(library) + new InputStreamEntryWriter(new FileInputStream(file), true) ); LibraryHasBeenWritten event = new LibraryHasBeenWritten(library); for (ArchiveWriterListener listener : listeners.get(LibraryHasBeenWritten.class)) { @@ -118,19 +114,13 @@ public void writeNestedLibrary( @Override public void writeEntries(JarFile jarFile) throws IOException { - this.writeEntries(jarFile, new SkipManifestMfTransformer(), NEVER_UNPACK); - } - - @Override - public void writeEntries(JarFile jarFile, UnpackHandler unpackHandler) throws IOException { - this.writeEntries(jarFile, new SkipManifestMfTransformer(), unpackHandler); + this.writeEntries(jarFile, new SkipManifestMfTransformer()); } @Override public void writeEntries( JarFile jarFile, - EntryTransformer entryTransformer, - UnpackHandler unpackHandler + EntryTransformer entryTransformer ) throws IOException { Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { @@ -141,7 +131,7 @@ public void writeEntries( EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); JarArchiveEntry transformedEntry = entryTransformer.transform(entry); if (transformedEntry != null) { - writeEntry(transformedEntry, entryWriter, unpackHandler); + writeEntry(transformedEntry, entryWriter); } } } @@ -211,26 +201,17 @@ public void close() throws IOException { this.jarOutput.close(); } - private void writeEntry( - JarArchiveEntry entry, - EntryWriter entryWriter - ) throws IOException { - writeEntry(entry, entryWriter, NEVER_UNPACK); - } - /** * Perform the actual write of a {@link JarEntry}. All other write methods delegate to * this one. * * @param entry the entry to write * @param entryWriter the entry writer or {@code null} if there is no content - * @param unpackHandler handles possible unpacking for the entry * @throws IOException in case of I/O errors */ private void writeEntry( JarArchiveEntry entry, - @Nullable EntryWriter entryWriter, - UnpackHandler unpackHandler + @Nullable EntryWriter entryWriter ) throws IOException { String parent = entry.getName(); boolean isDirectory = false; @@ -244,12 +225,11 @@ private void writeEntry( if (parent.lastIndexOf('/') != -1) { parent = parent.substring(0, parent.lastIndexOf('/') + 1); if (!parent.isEmpty()) { - writeEntry(new JarArchiveEntry(parent), null, unpackHandler); + writeEntry(new JarArchiveEntry(parent), null); } } if (this.writtenEntries.add(entry.getName())) { - entryWriter = addUnpackCommentIfNecessary(entry, entryWriter, unpackHandler); this.jarOutput.putArchiveEntry(entry); if (entryWriter != null) { entryWriter.write(this.jarOutput); @@ -265,21 +245,4 @@ private void writeEntry( } } - private static EntryWriter addUnpackCommentIfNecessary( - JarArchiveEntry entry, - @Nullable EntryWriter entryWriter, - UnpackHandler unpackHandler - ) throws IOException { - if (entryWriter == null || !unpackHandler.requiresUnpack(entry.getName())) { - return entryWriter; - } - ByteArrayOutputStream output = new ByteArrayOutputStream(); - entryWriter.write(output); - entry.setComment("UNPACK:" + unpackHandler.sha256Hash(entry.getName())); - return new InputStreamEntryWriter( - new ByteArrayInputStream(output.toByteArray()), - true - ); - } - } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java deleted file mode 100644 index d62d669..0000000 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryUnpackHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.core.jar; - -import pl.wavesoftware.plugs.tools.packager.core.model.Library; - -import java.io.IOException; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -final class LibraryUnpackHandler implements UnpackHandler { - - private final Library library; - - LibraryUnpackHandler(Library library) { - this.library = library; - } - - @Override - public boolean requiresUnpack(String name) { - return this.library.isUnpackRequired(); - } - - @Override - public String sha256Hash(String name) throws IOException { - return FileUtils.sha256Hash(this.library.getFile()); - } - -} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java deleted file mode 100644 index 3b4b2d7..0000000 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/NeverUnpackHandler.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.core.jar; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -final class NeverUnpackHandler implements UnpackHandler { - - @Override - public boolean requiresUnpack(String name) { - return false; - } - - @Override - public String sha256Hash(String name) { - throw new UnsupportedOperationException("Not supported"); - } - -} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java deleted file mode 100644 index 9f41775..0000000 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/UnpackHandler.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.core.jar; - -import java.io.IOException; - -/** - * An {@code UnpackHandler} determines whether or not unpacking is - * required and provides a SHA256 hash if required. - * - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -interface UnpackHandler { - boolean requiresUnpack(String name); - - String sha256Hash(String name) throws IOException; -} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java index fa818d0..361b742 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java @@ -32,7 +32,7 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -public final class WritableLibraries implements UnpackHandler { +public final class WritableLibraries { private final Map libraryEntryNames = new LinkedHashMap<>(); @@ -52,22 +52,6 @@ public WritableLibraries(Libraries libraries) throws IOException { }); } - @Override - public boolean requiresUnpack(String name) { - Library library = this.libraryEntryNames.get(name); - return library != null && library.isUnpackRequired(); - } - - @Override - public String sha256Hash(String name) throws IOException { - Library library = this.libraryEntryNames.get(name); - if (library == null) { - throw new IllegalArgumentException( - "No library found for entry name '" + name + "'"); - } - return FileUtils.sha256Hash(library.getFile()); - } - public void write(JarWriter writer) throws IOException { for (Map.Entry entry : this.libraryEntryNames.entrySet()) { writer.writeNestedLibrary( diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java index 6229288..76ce3d2 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java @@ -20,8 +20,7 @@ * A block of code to be executed */ @FunctionalInterface -public -interface CodeBlock { +public interface CodeBlock { /** * A simple execute command */ diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java index 6bb5327..dad32f6 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java @@ -32,7 +32,6 @@ public final class Library { private final String name; private final File file; private final LibraryScope scope; - private final boolean unpackRequired; /** * Create a new {@link Library}. @@ -40,17 +39,7 @@ public final class Library { * @param scope the scope of the library */ public Library(File file, LibraryScope scope) { - this(file, scope, false); - } - - /** - * Create a new {@link Library}. - * @param file the source file - * @param scope the scope of the library - * @param unpackRequired if the library needs to be unpacked before it can be used - */ - public Library(File file, LibraryScope scope, boolean unpackRequired) { - this(null, file, scope, unpackRequired); + this(null, file, scope); } /** @@ -59,18 +48,15 @@ public Library(File file, LibraryScope scope, boolean unpackRequired) { * the file name * @param file the source file * @param scope the scope of the library - * @param unpackRequired if the library needs to be unpacked before it can be used */ public Library( @Nullable String name, File file, - LibraryScope scope, - boolean unpackRequired + LibraryScope scope ) { this.name = (name != null) ? name : file.getName(); this.file = file; this.scope = scope; - this.unpackRequired = unpackRequired; } /** @@ -97,12 +83,4 @@ public LibraryScope getScope() { return this.scope; } - /** - * Return if the file cannot be used directly as a nested jar and needs to be - * unpacked. - * @return if unpack is required - */ - public boolean isUnpackRequired() { - return this.unpackRequired; - } } From af99f43522c3f64bd7e5103e3ba58053ad011bc2 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Fri, 29 Mar 2019 20:59:30 +0100 Subject: [PATCH 15/23] Resolving Maven dependencies --- tools/plugs-maven-plugin/pom.xml | 11 +++ .../tools/maven/plugin/PackagePlugMojo.java | 14 +++ .../maven/plugin/mapper/ArtifactMapper.java | 5 +- .../plugin/mapper/ArtifactMapperImpl.java | 36 ++++++-- .../maven/plugin/mapper/MavenArtifact.java | 7 ++ .../plugin/mapper/MavenBackedProject.java | 20 ++++- .../maven/plugin/mapper/MavenDependency.java | 68 --------------- .../mapper/MavenPackagerConfiguration.java | 5 ++ .../MavenPackagerConfigurationFactory.java | 3 + .../mapper/PackagerConfigurationFactory.java | 3 + .../plugin/model/ResolvableDependency.java | 49 +++++++++++ .../junit5/DefaultMojoConfigurator.java | 74 +++++++++++++++- .../maven/testing/junit5/MojoBuilderImpl.java | 3 + .../src/test/resources/code-with-deps/pom.xml | 58 ++++++++++++- .../wavesoftware/example/code/Activator.java | 54 ++++++++++++ .../pl/wavesoftware/example/code/Context.java | 31 +++++++ .../example/code/HelloService.java | 24 ++++-- .../example/code/HelloServiceImpl.java | 30 +++++++ .../example/code/package-info.java | 24 ++++++ .../example/code/ActivatorTest.java | 85 +++++++++++++++++++ tools/plugs-packager-core/pom.xml | 8 ++ .../tools/packager/core/DefaultPackager.java | 2 +- .../packager/core/DefaultPackagerFactory.java | 2 +- .../core/DefaultRepackagingIsRequired.java | 17 ++-- .../DefaultRepackagingIsRequiredFactory.java | 13 +-- .../core/RepackagingIsRequiredFactory.java | 4 +- .../ProjectDigest.java} | 13 +-- .../ProjectDigestImpl.java} | 18 ++-- .../core/manifest/ManifestBuilderImpl.java | 8 +- .../tools/packager/core/model/Project.java | 7 ++ .../META-INF/sisu/javax.inject.Named | 2 +- .../core/digest/ProjectDigestImplTest.java | 81 ++++++++++++++++++ .../packager/core/sample/ProjectSamples.java | 41 +++++++++ .../tools/packager/core/sample/Sample.java | 29 +++++++ .../core/sample/SimpleProjectSample.java | 30 +++++++ .../plugs/tools/packager/core/sample/pom.xml | 22 +++++ 36 files changed, 778 insertions(+), 123 deletions(-) delete mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/ResolvableDependency.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Activator.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Context.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloServiceImpl.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/package-info.java create mode 100644 tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/test/java/pl/wavesoftware/example/code/ActivatorTest.java rename tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/{jar/FileDigest.java => digest/ProjectDigest.java} (75%) rename tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/{jar/FileDigestImpl.java => digest/ProjectDigestImpl.java} (59%) create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java create mode 100644 tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index 10859e9..8dc3d5e 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -169,5 +169,16 @@ true + + + + maven-compiler-plugin + + + -Xlint:all,-processing + + + + diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java index 7134b18..451a1a2 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java @@ -16,6 +16,7 @@ package pl.wavesoftware.plugs.tools.maven.plugin; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -84,6 +85,18 @@ final class PackagePlugMojo extends AbstractMojo { ) private MavenProject project; + /** + * The Maven session. + * + * @since 0.1.0 + */ + @Parameter( + defaultValue = "${session}", + required = true, + readonly = true + ) + private MavenSession session; + /** * Classifier to add to the repackaged archive. If not given, classifier * "plug" will be used. If given, the classifier will also be used @@ -233,6 +246,7 @@ private void attachArtifactIfNeeded(PackagerConfiguration configuration) { private PackagerConfiguration createConfiguration() { return factories.configuration().create( project, + session, logger, classifier, attach, diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java index dd60134..601c749 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java @@ -16,7 +16,8 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; -import org.apache.maven.model.Dependency; +import io.vavr.collection.Traversable; +import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency; import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; /** @@ -26,5 +27,5 @@ public interface ArtifactMapper { Artifact generalize(org.apache.maven.artifact.Artifact artifact); org.apache.maven.artifact.Artifact mavenize(Artifact artifact); - Artifact map(Dependency dependency); + Traversable map(ResolvableDependency dependency); } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java index a0e46e5..59c7d3d 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java @@ -16,8 +16,15 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; -import org.apache.maven.model.Dependency; +import io.vavr.collection.HashSet; +import io.vavr.collection.Traversable; +import org.apache.maven.artifact.repository.DefaultRepositoryRequest; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.repository.RepositorySystem; +import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency; import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; import javax.inject.Inject; @@ -31,10 +38,15 @@ final class ArtifactMapperImpl implements ArtifactMapper { private final RepositorySystem repositorySystem; + private final ArtifactResolver artifactResolver; @Inject - ArtifactMapperImpl(RepositorySystem repositorySystem) { + ArtifactMapperImpl( + RepositorySystem repositorySystem, + ArtifactResolver artifactResolver + ) { this.repositorySystem = repositorySystem; + this.artifactResolver = artifactResolver; } @Override @@ -47,16 +59,26 @@ public org.apache.maven.artifact.Artifact mavenize(Artifact artifact) { if (artifact instanceof MavenArtifact) { return ((MavenArtifact) artifact).getDelegate(); } - if (artifact instanceof MavenDependency) { - return ((MavenDependency) artifact).asArtifact(); - } throw new UnsupportedOperationException( "Not supported artifact type: " + artifact.getClass() ); } @Override - public Artifact map(Dependency dependency) { - return new MavenDependency(repositorySystem, dependency); + public Traversable map(ResolvableDependency resolvable) { + return resolve(resolvable).map(this::generalize); + } + + private Traversable resolve(ResolvableDependency resolvable) { + RepositoryRequest repositoryRequest = DefaultRepositoryRequest.getRepositoryRequest( + resolvable.mavenSession(), resolvable.mavenProject() + ); + ArtifactResolutionRequest request = new ArtifactResolutionRequest(repositoryRequest); + request.setResolveTransitively(true); + org.apache.maven.artifact.Artifact artifact = + repositorySystem.createDependencyArtifact(resolvable.dependency()); + request.setArtifact(artifact); + ArtifactResolutionResult response = artifactResolver.resolve(request); + return HashSet.ofAll(response.getArtifacts()); } } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java index e710b40..9b82496 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java @@ -56,4 +56,11 @@ public Path path() { public ArtifactType type() { return ArtifactType.fromPackging(getDelegate().getType()); } + + @Override + public String toString() { + return "MavenArtifact{" + + name() + "@" + version() + + '}'; + } } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java index 01395ed..6a9cd50 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java @@ -19,8 +19,10 @@ import io.vavr.Lazy; import io.vavr.collection.HashSet; import io.vavr.collection.Set; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Dependency; import org.apache.maven.project.MavenProject; +import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency; import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; import pl.wavesoftware.plugs.tools.packager.core.model.Project; @@ -33,6 +35,7 @@ final class MavenBackedProject implements Project { private final ArtifactMapper artifactMapper; private final MavenProject mavenProject; + private final MavenSession mavenSession; private final Path path; private final String finalName; private final String classifier; @@ -43,12 +46,14 @@ final class MavenBackedProject implements Project { MavenBackedProject( ArtifactMapper artifactMapper, MavenProject mavenProject, + MavenSession mavenSession, Path path, String finalName, String classifier ) { this.artifactMapper = artifactMapper; this.mavenProject = mavenProject; + this.mavenSession = mavenSession; this.path = path; this.finalName = finalName; this.classifier = classifier; @@ -57,6 +62,11 @@ final class MavenBackedProject implements Project { importsLazy = Lazy.of(this::calculateImports); } + @Override + public Path buildFilePath() { + return mavenProject.getBasedir().toPath().resolve("pom.xml"); + } + @Override public Artifact mainArtifact() { return artifactMapper.generalize(mavenProject.getArtifact()); @@ -91,14 +101,20 @@ private Set calculateDependencies() { return HashSet .ofAll(mavenProject.getDependencies()) .reject(MavenBackedProject::hasProvidedScope) - .map(artifactMapper::map); + .map(this::asResolvable) + .flatMap(artifactMapper::map); } private Set calculateImports() { return HashSet .ofAll(mavenProject.getDependencies()) .filter(MavenBackedProject::hasProvidedScope) - .map(artifactMapper::map); + .map(this::asResolvable) + .flatMap(artifactMapper::map); + } + + private ResolvableDependency asResolvable(Dependency dependency) { + return new ResolvableDependency(dependency, mavenProject, mavenSession); } private static boolean hasProvidedScope(Dependency dependency) { diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java deleted file mode 100644 index 54c3ead..0000000 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenDependency.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; - -import com.github.zafarkhaja.semver.Version; -import io.vavr.Lazy; -import org.apache.maven.model.Dependency; -import org.apache.maven.repository.RepositorySystem; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; - -import java.nio.file.Path; - -final class MavenDependency implements Artifact { - private final RepositorySystem repositorySystem; - private final Dependency delegate; - private final Lazy artifactLazy = Lazy.of( - this::mapAsArtifact - ); - - MavenDependency(RepositorySystem repositorySystem, Dependency dependency) { - this.repositorySystem = repositorySystem; - this.delegate = dependency; - } - - @Override - public String name() { - return delegate.getArtifactId(); - } - - @Override - public Version version() { - return Version.valueOf(delegate.getVersion()); - } - - @Override - public Path path() { - org.apache.maven.artifact.Artifact artifact = asArtifact(); - return artifact.getFile().toPath(); - } - - @Override - public ArtifactType type() { - return ArtifactType.fromPackging(delegate.getType()); - } - - org.apache.maven.artifact.Artifact asArtifact() { - return artifactLazy.get(); - } - - private org.apache.maven.artifact.Artifact mapAsArtifact() { - return repositorySystem.createDependencyArtifact(delegate); - } -} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java index 8ebc379..50f131a 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java @@ -17,6 +17,7 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; import io.vavr.Lazy; +import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; @@ -35,6 +36,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration { private final ArtifactMapper artifactMapper; private final Logger logger; private final MavenProject mavenProject; + private final MavenSession mavenSession; private final Supplier project; private final String classifier; private final boolean attach; @@ -46,6 +48,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration { ArtifactMapper artifactMapper, Logger logger, MavenProject project, + MavenSession session, String classifier, boolean attach, File outputDirectory, @@ -54,6 +57,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration { this.artifactMapper = artifactMapper; this.logger = logger; this.mavenProject = project; + this.mavenSession = session; this.classifier = classifier; this.attach = attach; this.outputDirectory = outputDirectory; @@ -86,6 +90,7 @@ private Project calculateProject() { return new MavenBackedProject( artifactMapper, mavenProject, + mavenSession, outputDirectory.toPath(), finalName, classifier diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java index 8de55e6..a276375 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java @@ -16,6 +16,7 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; +import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; @@ -41,6 +42,7 @@ final class MavenPackagerConfigurationFactory implements PackagerConfigurationFa @Override public PackagerConfiguration create( MavenProject project, + MavenSession session, Logger logger, String classifier, boolean attach, @@ -51,6 +53,7 @@ public PackagerConfiguration create( artifactMapper, logger, project, + session, classifier, attach, outputDirectory, diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java index 0d53e16..75aaab3 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java @@ -16,6 +16,7 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; +import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; @@ -33,6 +34,7 @@ public interface PackagerConfigurationFactory { * Creates a configuration * * @param project a maven project + * @param session a maven session * @param logger a logger * @param classifier classifier of plug to create * @param attach should created plug be attached to reactor @@ -42,6 +44,7 @@ public interface PackagerConfigurationFactory { */ PackagerConfiguration create( MavenProject project, + MavenSession session, Logger logger, String classifier, boolean attach, diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/ResolvableDependency.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/ResolvableDependency.java new file mode 100644 index 0000000..0f98c5b --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/ResolvableDependency.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.model; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Dependency; +import org.apache.maven.project.MavenProject; + +public final class ResolvableDependency { + private final Dependency dependency; + private final MavenProject mavenProject; + private final MavenSession mavenSession; + + public ResolvableDependency( + Dependency dependency, + MavenProject mavenProject, + MavenSession mavenSession + ) { + this.dependency = dependency; + this.mavenProject = mavenProject; + this.mavenSession = mavenSession; + } + + public Dependency dependency() { + return dependency; + } + + public MavenProject mavenProject() { + return mavenProject; + } + + public MavenSession mavenSession() { + return mavenSession; + } +} diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java index 00d0cfb..6dc4036 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/DefaultMojoConfigurator.java @@ -16,15 +16,35 @@ package pl.wavesoftware.maven.testing.junit5; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequestPopulationException; +import org.apache.maven.execution.MavenExecutionRequestPopulator; +import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.testing.MojoRule; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.repository.internal.MavenRepositorySystemUtils; +import org.apache.maven.settings.MavenSettingsBuilder; +import org.apache.maven.settings.Settings; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import java.io.File; +import java.io.IOException; import java.nio.file.Path; +import java.util.Collections; import java.util.Optional; import java.util.function.Supplier; +import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; + /** * @author Krzysztof Suszynski * @since 0.1.0 @@ -39,13 +59,18 @@ final class DefaultMojoConfigurator implements MojoConfigurator { @Override public MavenSession getMavenSession(MojoRule rule, Path pomDirectory) throws Exception { + // create execution request + MavenExecutionRequest request = createMavenExecutionRequest(rule); + // setup with pom - MavenProject project = rule.readMavenProject(pomDirectory.toFile()); + MavenProject project = readMavenProject( + rule, request, pomDirectory.toFile() + ); customizer.get().ifPresent(customizer -> customizer.customize(project)); // Generate session - return rule.newMavenSession(project); + return newMavenSession(rule, request, project); } @Override @@ -53,4 +78,49 @@ public MojoExecution getMojoExecution(MojoRule rule, String goal) { // Generate PackagerConfiguration and Mojo for testing return rule.newMojoExecution(goal); } + + private MavenExecutionRequest createMavenExecutionRequest(MojoRule rule) + throws ComponentLookupException, IOException, + XmlPullParserException, MavenExecutionRequestPopulationException { + + PlexusContainer container = rule.getContainer(); + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + MavenExecutionRequestPopulator requestPopulator = + container.lookup(MavenExecutionRequestPopulator.class); + MavenSettingsBuilder settingsBuilder = container.lookup(MavenSettingsBuilder.class); + Settings settings = settingsBuilder.buildSettings(); + requestPopulator.populateFromSettings(request, settings); + requestPopulator.populateDefaults(request); + return request; + } + + private MavenProject readMavenProject( + MojoRule rule, MavenExecutionRequest request, File basedir + ) throws Exception { + File pom = new File(basedir, "pom.xml"); + request.setBaseDirectory(basedir); + ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); + configuration.setRepositorySession(new DefaultRepositorySystemSession()); + MavenProject project = rule.lookup(ProjectBuilder.class) + .build(pom, configuration) + .getProject(); + checkNotNull(project, "20190331:130819"); + return project; + } + + private MavenSession newMavenSession( + MojoRule rule, MavenExecutionRequest request, MavenProject project + ) { + MavenExecutionResult result = new DefaultMavenExecutionResult(); + + MavenSession session = new MavenSession( + rule.getContainer(), + MavenRepositorySystemUtils.newSession(), + request, + result + ); + session.setCurrentProject(project); + session.setProjects(Collections.singletonList(project)); + return session; + } } diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java index cb157fb..681a902 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoBuilderImpl.java @@ -18,6 +18,7 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.LegacySupport; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.testing.MojoRule; @@ -72,6 +73,8 @@ mojoRule, getPomDirectory() mojoRule, goal ); + LegacySupport legacySupport = mojoRule.lookup(LegacySupport.class); + legacySupport.setSession(session); org.apache.maven.plugin.Mojo mojo = mojoRule.lookupConfiguredMojo(session, execution); return type.cast(mojo); diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml index 91a85a8..70dab07 100644 --- a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml @@ -16,16 +16,27 @@ 4.0.0 + + org.springframework.boot + spring-boot-parent + 2.1.3.RELEASE + + org.example code-with-deps 0.1.0 + + UTF-8 + UTF-8 + 1.8 + ${maven.compiler.source} + + - org.springframework - spring-context - 5.1.5.RELEASE - provided + org.springframework.boot + spring-boot-autoconfigure @@ -33,5 +44,44 @@ apiguardian-api 1.0.0 + + + com.google.code.findbugs + jsr305 + 3.0.2 + + + + org.osgi + osgi.core + 7.0.0 + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.platform + junit-platform-launcher + test + + + org.mockito + mockito-junit-jupiter + test + + + org.assertj + assertj-core + test + diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Activator.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Activator.java new file mode 100644 index 0000000..74f2e81 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Activator.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.example.code; + +import org.apiguardian.api.API; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import javax.annotation.Nullable; +import java.util.Optional; + +import static org.apiguardian.api.API.Status.STABLE; + +@API(status = STABLE) +final class Activator implements BundleActivator { + + @Nullable + private ConfigurableApplicationContext applicationContext; + @Nullable + private ServiceRegistration registration; + + public void start(BundleContext context) { + applicationContext = new AnnotationConfigApplicationContext(Context.class); + registration = context.registerService( + HelloService.class, + applicationContext.getBean(HelloService.class), + null + ); + } + + public void stop(BundleContext context) { + Optional.ofNullable(registration) + .ifPresent(ServiceRegistration::unregister); + Optional.ofNullable(applicationContext) + .ifPresent(ConfigurableApplicationContext::close); + } +} diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Context.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Context.java new file mode 100644 index 0000000..73e6396 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/Context.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.example.code; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@SpringBootApplication +class Context { + public static void main(String[] args) { + SpringApplication.run(Context.class, args); + } +} diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java index babf1b5..f4326d0 100644 --- a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloService.java @@ -1,14 +1,26 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.example.code; import org.apiguardian.api.API; -import org.springframework.stereotype.Service; import static org.apiguardian.api.API.Status.STABLE; -@Service @API(status = STABLE) -public class HelloService { - String hello() { - return "Hello from Plug"; - } +public interface HelloService { + String hello(); } diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloServiceImpl.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloServiceImpl.java new file mode 100644 index 0000000..7201c2b --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/HelloServiceImpl.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.example.code; + +import org.springframework.stereotype.Service; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Service +final class HelloServiceImpl implements HelloService { + public String hello() { + return "Hello from Plug!"; + } +} diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/package-info.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/package-info.java new file mode 100644 index 0000000..6fcbcf5 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/main/java/pl/wavesoftware/example/code/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ParametersAreNonnullByDefault +package pl.wavesoftware.example.code; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/test/java/pl/wavesoftware/example/code/ActivatorTest.java b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/test/java/pl/wavesoftware/example/code/ActivatorTest.java new file mode 100644 index 0000000..7a02233 --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/src/test/java/pl/wavesoftware/example/code/ActivatorTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.example.code; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith(MockitoExtension.class) +class ActivatorTest { + + private Activator activator = new Activator(); + + @Mock + private BundleContext bundleContext; + + @Mock + private ServiceRegistration serviceRegistration; + + @Captor + private ArgumentCaptor helloServiceArgumentCaptor; + + @Test + void startAndStop() { + // then + verify(bundleContext).registerService( + eq(HelloService.class), + helloServiceArgumentCaptor.capture(), + isNull() + ); + HelloService service = helloServiceArgumentCaptor.getValue(); + assertThat(service.hello()).isEqualTo("Hello from Plug!"); + } + + @BeforeEach + void before() { + when(bundleContext.registerService( + eq(HelloService.class), any(HelloService.class), any() + )).thenReturn(serviceRegistration); + activator.start(bundleContext); + } + + @AfterEach + void after() { + activator.stop(bundleContext); + verify(serviceRegistration).unregister(); + + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(bundleContext, serviceRegistration); + } + +} diff --git a/tools/plugs-packager-core/pom.xml b/tools/plugs-packager-core/pom.xml index 597f686..188dd09 100644 --- a/tools/plugs-packager-core/pom.xml +++ b/tools/plugs-packager-core/pom.xml @@ -65,6 +65,14 @@ org.apache.commons commons-compress + + + + pl.wavesoftware.plugs + testing + ${project.version} + test + diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java index 1d580ac..e7e129a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java @@ -188,7 +188,7 @@ private void repackage( } } - private JarWriter newJarWriter(Path destination) throws RepackageFailed { + private static JarWriter newJarWriter(Path destination) throws RepackageFailed { return tring(() -> new JarWriter(destination)).or( "Can't create a jar at destination: {}", destination diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java index ced55b5..488be1d 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java @@ -53,7 +53,7 @@ public Packager create( Filter filter ) { RepackagingIsRequired condition = conditionFactory.create( - configuration.coordinates() + configuration.coordinates(), configuration.project() ); return new DefaultPackager( configuration, diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java index 366a836..cd7f33a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java @@ -16,8 +16,9 @@ package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; +import pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigest; import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; import java.io.File; @@ -33,18 +34,20 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -final class DefaultRepackagingIsRequired - implements RepackagingIsRequired { +final class DefaultRepackagingIsRequired implements RepackagingIsRequired { private final PackagerCoordinates coordinates; - private final FileDigest fileDigest; + private final Project project; + private final ProjectDigest projectDigest; DefaultRepackagingIsRequired( PackagerCoordinates coordinates, - FileDigest fileDigest + Project project, + ProjectDigest projectDigest ) { this.coordinates = coordinates; - this.fileDigest = fileDigest; + this.project = project; + this.projectDigest = projectDigest; } @Override @@ -60,7 +63,7 @@ private boolean alreadyRepackaged() throws IOException { } try (JarFile jarFile = new JarFile(targetFile)) { Manifest manifest = jarFile.getManifest(); - CharSequence digest = fileDigest.digest(sourcePath); + CharSequence digest = projectDigest.digest(project); return manifest != null && digest.equals(manifest.getMainAttributes().getValue(PLUGS_DIGEST_ATTRIBUTE)); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java index cea2407..f68c319 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java @@ -16,8 +16,9 @@ package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; +import pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigest; import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; import javax.inject.Inject; @@ -31,16 +32,18 @@ final class DefaultRepackagingIsRequiredFactory implements RepackagingIsRequiredFactory { - private final FileDigest digest; + private final ProjectDigest digest; @Inject - DefaultRepackagingIsRequiredFactory(FileDigest digest) { + DefaultRepackagingIsRequiredFactory(ProjectDigest digest) { this.digest = digest; } @Override - public RepackagingIsRequired create(PackagerCoordinates coordinates) { - return new DefaultRepackagingIsRequired(coordinates, digest); + public RepackagingIsRequired create( + PackagerCoordinates coordinates, Project project + ) { + return new DefaultRepackagingIsRequired(coordinates, project, digest); } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java index 4c4b5f7..7d86a6e 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java @@ -17,6 +17,7 @@ package pl.wavesoftware.plugs.tools.packager.core; import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; /** @@ -30,7 +31,8 @@ public interface RepackagingIsRequiredFactory { * Create a condition that is true if packaging is required. * * @param coordinates a coordinates of operation + * @param project a project * @return a condition */ - RepackagingIsRequired create(PackagerCoordinates coordinates); + RepackagingIsRequired create(PackagerCoordinates coordinates, Project project); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java similarity index 75% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java index e1b4331..d3bc65f 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigest.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.jar; +package pl.wavesoftware.plugs.tools.packager.core.digest; + +import pl.wavesoftware.plugs.tools.packager.core.model.Project; import java.io.IOException; -import java.nio.file.Path; /** * A calculator of a digest of a file @@ -25,13 +26,13 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface FileDigest { +public interface ProjectDigest { /** - * Digest a path to a file + * Digest a project * - * @param path a path to digest + * @param project a project to digest * @return a digest * @throws IOException if cant be read */ - CharSequence digest(Path path) throws IOException; + CharSequence digest(Project project) throws IOException; } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImpl.java similarity index 59% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImpl.java index adab60e..aa0de7b 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileDigestImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImpl.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.jar; +package pl.wavesoftware.plugs.tools.packager.core.digest; + +import pl.wavesoftware.plugs.tools.packager.core.model.Project; import javax.inject.Named; import java.io.IOException; @@ -30,13 +32,17 @@ * @since 0.1.0 */ @Named -final class FileDigestImpl implements FileDigest { +final class ProjectDigestImpl implements ProjectDigest { @Override - public CharSequence digest(Path path) throws IOException { - checkArgument(path.toFile().isFile(), "20190131:221929"); + public CharSequence digest(Project project) throws IOException { + Path sourcePath = project.mainArtifact().path(); + Path buildFilePath = project.buildFilePath(); + checkArgument(sourcePath.toFile().isFile(), "20190131:221929"); CRC32 digest = new CRC32(); - digest.update(path.toAbsolutePath().toString().getBytes(UTF_8)); - digest.update(Long.toHexString(Files.size(path)).getBytes(UTF_8)); + digest.update(sourcePath.toAbsolutePath().toString().getBytes(UTF_8)); + digest.update(Long.toHexString(Files.size(sourcePath)).getBytes(UTF_8)); + digest.update(Long.toHexString(Files.size(buildFilePath)).getBytes(UTF_8)); + digest.update(Long.toHexString(buildFilePath.toFile().lastModified()).getBytes(UTF_8)); return Long.toHexString(Math.abs(digest.getValue())); } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java index e96b762..3947eaf 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java @@ -19,7 +19,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.wavesoftware.plugs.api.PlugsVersion; -import pl.wavesoftware.plugs.tools.packager.core.jar.FileDigest; +import pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigest; import pl.wavesoftware.plugs.tools.packager.core.model.Project; import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; @@ -44,10 +44,10 @@ final class ManifestBuilderImpl implements ManifestBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(ManifestBuilderImpl.class); - private final FileDigest digest; + private final ProjectDigest digest; @Inject - ManifestBuilderImpl(FileDigest digest) { + ManifestBuilderImpl(ProjectDigest digest) { this.digest = digest; } @@ -69,7 +69,7 @@ public Manifest buildManifest( manifest = new Manifest(manifest); Attributes attributes = manifest.getMainAttributes(); String plugsVersion = PlugsVersion.getVersion(); - CharSequence hash = tring(() -> digest.digest(sourcePath)).or( + CharSequence hash = tring(() -> digest.digest(project)).or( "Can't calculate digest from source jar: {}", sourcePath ); diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java index 2b3804b..3b77ab6 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java @@ -28,6 +28,13 @@ */ public interface Project { + /** + * A build file path. For ex.: pom.xml + * + * @return a build file path + */ + Path buildFilePath(); + /** * A main artifact of a project * diff --git a/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named b/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named index 18def04..4fed7bf 100644 --- a/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named +++ b/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named @@ -1,4 +1,4 @@ -pl.wavesoftware.plugs.tools.packager.core.jar.FileDigestImpl +pl.wavesoftware.plugs.tools.packager.core.jar.ProjectDigestImpl pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilderImpl pl.wavesoftware.plugs.tools.packager.core.DefaultPackagerFactory pl.wavesoftware.plugs.tools.packager.core.DefaultRepackagingIsRequiredFactory diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java new file mode 100644 index 0000000..170c3aa --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.digest; + +import com.github.zafarkhaja.semver.Version; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.core.model.Project; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith(MockitoExtension.class) +class ProjectDigestImplTest { + + @Mock + Project project; + + @Test + void digest() throws IOException, URISyntaxException { + // given + ProjectDigest digester = new ProjectDigestImpl(); + Path buildFilePath = Paths.get(getClass().getResource("pom.xml").toURI()); + Artifact artifact = new Artifact() { + @Override + public String name() { + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public Version version() { + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public Path path() { + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public ArtifactType type() { + throw new UnsupportedOperationException("Not yet implemented"); + } + }; + when(project.buildFilePath()).thenReturn(buildFilePath); + when(project.mainArtifact()).thenReturn(artifact); + + // when + CharSequence digest = digester.digest(project); + + // then + assertThat(digest).isEqualTo("22ssdd33"); + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java new file mode 100644 index 0000000..4905d6f --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.sample; + +import pl.wavesoftware.plugs.tools.packager.core.model.Project; + +import java.util.function.Supplier; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public enum ProjectSamples implements Sample { + + SIMPLE_PROJECT(new SimpleProjectSample()); + + private final Supplier project; + + ProjectSamples(Supplier project) { + this.project = project; + } + + @Override + public Project get() { + return project.get(); + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java new file mode 100644 index 0000000..189badb --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.sample; + +import java.util.function.Supplier; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +public interface Sample extends Supplier { + default T getSample() { + return get(); + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java new file mode 100644 index 0000000..3cace72 --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.sample; + +import pl.wavesoftware.plugs.tools.packager.core.model.Project; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class SimpleProjectSample implements Sample { + @Override + public Project get() { + throw new UnsupportedOperationException("Not yet implemented"); + } +} diff --git a/tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml b/tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml new file mode 100644 index 0000000..9246447 --- /dev/null +++ b/tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml @@ -0,0 +1,22 @@ + + + + 4.0.0 + org.example + digest-test + 0.1.0 + From 4c93495ccdf907fa0935999223cc5ec15be7b0a7 Mon Sep 17 00:00:00 2001 From: Krzysztof Suszynski Date: Mon, 20 May 2019 19:51:03 +0200 Subject: [PATCH 16/23] Adding plugs packager samples and testing modules * Added a sampler library and use it to create samples * Introduce API nad DEVEL modules for plugs packager --- .travis.yml | 2 +- plug-api/pom.xml | 11 +- .../plugs/api/PlugsVersionIT.java | 12 +- plugs-core/pom.xml | 7 +- plugs-felix/pom.xml | 7 +- plugs-spring/pom.xml | 7 +- pom.xml | 30 ++- testing/pom.xml | 94 --------- .../plugs/testing/ansi/AnsiBackground.java | 59 ------ .../plugs/testing/ansi/AnsiColor.java | 59 ------ .../plugs/testing/ansi/AnsiComposite.java | 54 ------ .../plugs/testing/ansi/AnsiElement.java | 35 ---- .../plugs/testing/ansi/AnsiOutput.java | 176 ----------------- .../plugs/testing/ansi/AnsiStyle.java | 47 ----- .../log4j2/HighlightColorConverter.java | 179 ------------------ testing/src/main/resources/log4j2-test.xml | 37 ---- .../plugs/testing/ansi/AnsiOutputTest.java | 123 ------------ .../testing/log4j2/CollectorAppender.java | 94 --------- .../testing/log4j2/CollectorManager.java | 70 ------- .../log4j2/HighlightColorConverterTest.java | 149 --------------- testing/src/test/resources/log4j2-test.xml | 36 ---- tools/plugs-maven-plugin/pom.xml | 7 +- .../tools/maven/plugin/PackagePlugMojo.java | 12 +- .../tools/maven/plugin/PackagerFactories.java | 2 +- .../maven/plugin/PackagerFactoriesImpl.java | 2 +- .../filter/AbstractDependencyFilter.java | 2 +- .../maven/plugin/filter/ExcludeFilter.java | 2 +- .../maven/plugin/filter/FilterFactory.java | 2 +- .../maven/plugin/filter/IncludeFilter.java | 2 +- .../maven/plugin/filter/MavenFilter.java | 6 +- .../plugin/filter/MavenFilterFactory.java | 2 +- .../maven/plugin/filter/package-info.java | 3 + .../maven/plugin/mapper/ArtifactMapper.java | 2 +- .../plugin/mapper/ArtifactMapperImpl.java | 2 +- .../maven/plugin/mapper/MavenArtifact.java | 12 +- .../plugin/mapper/MavenBackedProject.java | 4 +- .../plugin/mapper/MavenLibrariesFactory.java | 6 +- .../mapper/MavenPackagerConfiguration.java | 6 +- .../MavenPackagerConfigurationFactory.java | 2 +- .../mapper/MavenPackagerCoordinates.java | 6 +- .../mapper/PackagerConfigurationFactory.java | 2 +- .../maven/plugin/mapper/package-info.java | 3 + .../model/AbstractFilterableDependency.java | 2 +- .../maven/plugin/model/MavenLibraries.java | 8 +- .../maven/plugin/model/package-info.java | 3 + .../src/test/resources/code-with-deps/pom.xml | 2 +- tools/plugs-packager-api/pom.xml | 71 +++++++ .../plugs/tools/packager/api}/Constants.java | 2 +- .../plugs/tools/packager/api}/Packager.java | 4 +- .../tools/packager/api}/PackagerFactory.java | 6 +- .../api}/RepackagingIsRequiredFactory.java | 8 +- .../packager/api/digest/ProjectDigester.java} | 6 +- .../packager/api/digest}/package-info.java | 10 +- .../packager/api}/jar/ArchiveWriterEvent.java | 2 +- .../api}/jar/ArchiveWriterListener.java | 2 +- .../packager/api}/jar/LibrariesCollector.java | 4 +- .../tools/packager/api/jar}/package-info.java | 10 +- .../api}/manifest/ManifestBuilder.java | 6 +- .../packager/api/manifest/package-info.java | 23 +++ .../tools/packager/api}/model/Artifact.java | 6 +- .../packager/api}/model/ArtifactType.java | 4 +- .../tools/packager/api}/model/CodeBlock.java | 2 +- .../tools/packager/api}/model/Filter.java | 2 +- .../api}/model/FilterableDependency.java | 2 +- .../packager/api}/model/IoPossibleBlock.java | 2 +- .../api}/model/IoPossibleSupplier.java | 2 +- .../tools/packager/api}/model/Libraries.java | 2 +- .../tools/packager/api}/model/Library.java | 2 +- .../packager/api}/model/LibraryCallback.java | 2 +- .../packager/api}/model/LibraryScope.java | 2 +- .../plugs/tools/packager/api}/model/Or.java | 2 +- .../api}/model/PackagerConfiguration.java | 2 +- .../api}/model/PackagerCoordinates.java | 2 +- .../tools/packager/api}/model/Project.java | 2 +- .../packager/api}/model/RepackageFailed.java | 2 +- .../api}/model/RepackagingIsRequired.java | 2 +- .../api}/model/StandardLibraries.java | 2 +- .../packager/api/model}/package-info.java | 5 +- .../tools/packager/api/package-info.java | 23 +++ .../packager/api}/spi/LibrariesFactory.java | 6 +- .../tools/packager/api/spi}/package-info.java | 5 +- .../api/jar/LibrariesCollectorTest.java | 53 ++++++ .../packager/api/model/ArtifactTypeTest.java | 52 +++++ .../api/model/RepackageFailedTest.java | 95 ++++++++++ tools/plugs-packager-core/pom.xml | 32 ++-- .../tools/packager/core/DefaultPackager.java | 31 +-- .../packager/core/DefaultPackagerFactory.java | 13 +- .../core/DefaultRepackagingIsRequired.java | 18 +- .../DefaultRepackagingIsRequiredFactory.java | 13 +- ...gestImpl.java => ProjectDigesterImpl.java} | 25 ++- .../packager/core/digest/package-info.java | 23 +++ .../packager/core/jar/ArchiveWriter.java | 5 +- .../tools/packager/core/jar/JarWriter.java | 4 +- .../core/jar/LibraryHasBeenWritten.java | 3 +- .../packager/core/jar/WritableLibraries.java | 6 +- .../tools/packager/core/jar/package-info.java | 3 + .../core/manifest/ManifestBuilderImpl.java | 24 +-- .../packager/core/manifest/package-info.java | 3 + .../tools/packager/core/package-info.java | 3 + .../META-INF/sisu/javax.inject.Named | 2 +- .../core/digest/ProjectDigestImplTest.java | 81 -------- .../core/digest/ProjectDigesterImplTest.java | 57 ++++++ .../manifest/ManifestBuilderImplTest.java | 101 ++++++++++ .../core/sample/SimpleProjectSample.java | 30 --- .../plugs/tools/packager/core/sample/pom.xml | 22 --- tools/plugs-packager-samples/pom.xml | 69 +++++++ .../sample/PackagerSamplerContext.java} | 19 +- .../sample/artifact/HibernateArtifact.java | 24 +++ .../sample/artifact/Jsr305Artifact.java | 23 +++ .../sample/artifact/OsgiCoreArtifact.java | 23 +++ .../artifact/SimpleProjectBuildArtifact.java | 23 +++ .../artifact/impl/HibernateArtifactImpl.java | 42 ++++ .../artifact/impl/Jsr305ArtifactImpl.java | 42 ++++ .../artifact/impl/MavenlikeArtifact.java | 84 ++++++++ .../artifact/impl/OsgiCoreArtifactImpl.java | 42 ++++ .../impl/SimpleProjectBuildArtifactImpl.java | 89 +++++++++ .../sample/artifact/impl/package-info.java | 23 +++ .../sample/artifact/package-info.java | 23 +++ .../tools/packager/sample/fs/VirtualRoot.java | 26 +++ .../packager/sample/fs/VirtualRootImpl.java} | 31 ++- .../packager/sample/fs/package-info.java | 23 +++ .../tools/packager/sample/package-info.java | 23 +++ .../sample/project/AbstractProject.java | 83 ++++++++ .../sample/project/SimpleProject.java | 23 +++ .../sample/project/SimpleProjectImpl.java | 67 +++++++ .../packager/sample/project/package-info.java | 23 +++ .../sample/PackagerSamplerContextIT.java | 81 ++++++++ tools/pom.xml | 2 + 128 files changed, 1640 insertions(+), 1592 deletions(-) delete mode 100644 testing/pom.xml delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiBackground.java delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiColor.java delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiComposite.java delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiElement.java delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutput.java delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiStyle.java delete mode 100644 testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java delete mode 100644 testing/src/main/resources/log4j2-test.xml delete mode 100644 testing/src/test/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutputTest.java delete mode 100644 testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorAppender.java delete mode 100644 testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorManager.java delete mode 100644 testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java delete mode 100644 testing/src/test/resources/log4j2-test.xml create mode 100644 tools/plugs-packager-api/pom.xml rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/Constants.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/Packager.java (92%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/PackagerFactory.java (85%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/RepackagingIsRequiredFactory.java (80%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/ProjectDigester.java} (86%) rename {testing/src/main/java/pl/wavesoftware/plugs/testing/ansi => tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest}/package-info.java (81%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/jar/ArchiveWriterEvent.java (93%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/jar/ArchiveWriterListener.java (94%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/jar/LibrariesCollector.java (90%) rename {testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2 => tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar}/package-info.java (80%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/manifest/ManifestBuilder.java (86%) create mode 100644 tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/package-info.java rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/Artifact.java (90%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/ArtifactType.java (91%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/CodeBlock.java (92%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/Filter.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/FilterableDependency.java (93%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/IoPossibleBlock.java (93%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/IoPossibleSupplier.java (94%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/Libraries.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/Library.java (97%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/LibraryCallback.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/LibraryScope.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/Or.java (94%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/PackagerConfiguration.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/PackagerCoordinates.java (95%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/Project.java (96%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/RepackageFailed.java (97%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/RepackagingIsRequired.java (94%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/model/StandardLibraries.java (94%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model}/package-info.java (84%) create mode 100644 tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/package-info.java rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api}/spi/LibrariesFactory.java (86%) rename tools/{plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model => plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi}/package-info.java (84%) create mode 100644 tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java create mode 100644 tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactTypeTest.java create mode 100644 tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java rename tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/{ProjectDigestImpl.java => ProjectDigesterImpl.java} (58%) create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/package-info.java delete mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImplTest.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java delete mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java delete mode 100644 tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml create mode 100644 tools/plugs-packager-samples/pom.xml rename tools/{plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java => plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java} (59%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/package-info.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java rename tools/{plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java => plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java} (52%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/package-info.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/package-info.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/AbstractProject.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/package-info.java create mode 100644 tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContextIT.java diff --git a/.travis.yml b/.travis.yml index 873fef8..e60d1fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: java sudo: false dist: trusty -install: mvn install dependency:go-offline -DskipTests=true -Dmaven.javadoc.skip=true -B -V +install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V script: mvn clean verify --fail-at-end -U -V notifications: email: diff --git a/plug-api/pom.xml b/plug-api/pom.xml index fa0dbfa..70d0ec7 100644 --- a/plug-api/pom.xml +++ b/plug-api/pom.xml @@ -43,14 +43,15 @@ - pl.wavesoftware.plugs - testing - ${project.version} + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom test - com.github.zafarkhaja - java-semver + com.vdurmont + semver4j test diff --git a/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java index 6efacd1..85c7bf5 100644 --- a/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java +++ b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java @@ -16,26 +16,26 @@ package pl.wavesoftware.plugs.api; -import com.github.zafarkhaja.semver.Version; +import com.vdurmont.semver4j.Semver; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class PlugsVersionIT { - private static final Version BASE = Version.forIntegers(0, 0, 0); + private static final Semver BASE = new Semver("0.0.0"); @Test void getVersion() { - Version version = Version.valueOf(PlugsVersion.getVersion()); + Semver version = new Semver(PlugsVersion.getVersion()); - assertThat(version.greaterThanOrEqualTo(BASE)).isTrue(); + assertThat(version.isGreaterThanOrEqualTo(BASE)).isTrue(); } @Test void manually() { - Version version = Version.valueOf(PlugsVersion.manuallyRead()); + Semver version = new Semver(PlugsVersion.manuallyRead()); - assertThat(version.greaterThanOrEqualTo(BASE)).isTrue(); + assertThat(version.isGreaterThanOrEqualTo(BASE)).isTrue(); } } diff --git a/plugs-core/pom.xml b/plugs-core/pom.xml index 94246a2..08ebbd7 100644 --- a/plugs-core/pom.xml +++ b/plugs-core/pom.xml @@ -63,9 +63,10 @@ - pl.wavesoftware.plugs - testing - ${project.version} + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom test diff --git a/plugs-felix/pom.xml b/plugs-felix/pom.xml index 9efc251..173f886 100644 --- a/plugs-felix/pom.xml +++ b/plugs-felix/pom.xml @@ -47,9 +47,10 @@ - pl.wavesoftware.plugs - testing - ${project.version} + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom test diff --git a/plugs-spring/pom.xml b/plugs-spring/pom.xml index 2e33397..9eee6a3 100644 --- a/plugs-spring/pom.xml +++ b/plugs-spring/pom.xml @@ -58,9 +58,10 @@ - pl.wavesoftware.plugs - testing - ${project.version} + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom test diff --git a/pom.xml b/pom.xml index 8ba309b..2feccdc 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + + org.springframework.boot + spring-boot-dependencies + 2.1.5.RELEASE + + pl.wavesoftware.plugs plugs-parent 0.1.0-SNAPSHOT @@ -60,14 +66,12 @@ plugs-spring boms-parent examples - testing tools scm:git:https://github.com/wavesoftware/plugs.git - scm:git:git@github.com:wavesoftware/plugs.git - + scm:git:git@github.com:wavesoftware/plugs.git https://github.com/wavesoftware/plugs @@ -113,20 +117,12 @@ ${skipTests} - 2.1.2.RELEASE - 6.0.2 + 6.0.3 3.6.0 - - org.springframework.boot - spring-boot-dependencies - ${spring.boot.version} - pom - import - org.apiguardian apiguardian-api @@ -163,9 +159,9 @@ 2.0.0 - com.github.zafarkhaja - java-semver - 0.9.0 + com.vdurmont + semver4j + 2.2.0 org.apache.commons @@ -294,7 +290,7 @@ maven-surefire-plugin - 2.22.1 + 2.22.2 false @@ -302,7 +298,7 @@ maven-failsafe-plugin - 2.22.1 + 2.22.2 false diff --git a/testing/pom.xml b/testing/pom.xml deleted file mode 100644 index d8ebb4a..0000000 --- a/testing/pom.xml +++ /dev/null @@ -1,94 +0,0 @@ - - - - - 4.0.0 - - plugs-parent - pl.wavesoftware.plugs - 0.1.0-SNAPSHOT - - testing - Plugs :: Testing - - - ${project.basedir}/.. - false - - - - - org.springframework.boot - spring-boot - provided - - - pl.wavesoftware - eid-exceptions - - - org.apache.logging.log4j - log4j-slf4j-impl - - - org.apache.logging.log4j - log4j-core - - - com.portingle - slf4jtesting - runtime - - - org.junit.platform - junit-platform-launcher - - - org.junit.jupiter - junit-jupiter-engine - - - org.junit.vintage - junit-vintage-engine - - - org.junit.jupiter - junit-jupiter-params - - - org.assertj - assertj-core - - - org.mockito - mockito-junit-jupiter - - - org.awaitility - awaitility - runtime - - - io.vavr - vavr - - - diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiBackground.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiBackground.java deleted file mode 100644 index 8636694..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiBackground.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -/** - * {@link AnsiElement Ansi} background colors. - * - * @author Krzysztof Suszynski - * @author Phillip Webb (Spring Boot project) - * @author Geoffrey Chandler (Spring Boot project) - * @since 0.1.0 - * @see org.springframework.boot.ansi.AnsiBackground - */ -public enum AnsiBackground implements AnsiElement { - - DEFAULT("49"), - BLACK("40"), - RED("41"), - GREEN("42"), - YELLOW("43"), - BLUE("44"), - MAGENTA("45"), - CYAN("46"), - WHITE("47"), - BRIGHT_BLACK("100"), - BRIGHT_RED("101"), - BRIGHT_GREEN("102"), - BRIGHT_YELLOW("103"), - BRIGHT_BLUE("104"), - BRIGHT_MAGENTA("105"), - BRIGHT_CYAN("106"), - BRIGHT_WHITE("107"); - - private String code; - - AnsiBackground(String code) { - this.code = code; - } - - @Override - public String toString() { - return this.code; - } - -} diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiColor.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiColor.java deleted file mode 100644 index c91f6d3..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiColor.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -/** - * {@link AnsiElement Ansi} colors. - * - * @author Krzysztof Suszynski - * @author Phillip Webb (Spring Boot project) - * @author Geoffrey Chandler (Spring Boot project) - * @since 0.1.0 - * @see org.springframework.boot.ansi.AnsiColor - */ -public enum AnsiColor implements AnsiElement { - - DEFAULT("39"), - BLACK("30"), - RED("31"), - GREEN("32"), - YELLOW("33"), - BLUE("34"), - MAGENTA("35"), - CYAN("36"), - WHITE("37"), - - BRIGHT_BLACK("90"), - BRIGHT_RED("91"), - BRIGHT_GREEN("92"), - BRIGHT_YELLOW("93"), - BRIGHT_BLUE("94"), - BRIGHT_MAGENTA("95"), - BRIGHT_CYAN("96"), - BRIGHT_WHITE("97"); - - private final String code; - - AnsiColor(String code) { - this.code = code; - } - - @Override - public String toString() { - return this.code; - } -} diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiComposite.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiComposite.java deleted file mode 100644 index 805524c..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiComposite.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -/** - * An ANSI composite color code, that consists of multiple {@link AnsiElement}s - * - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -public final class AnsiComposite implements AnsiElement { - private static final CharSequence ENCODE_JOIN = ";"; - - private final List elements; - - private AnsiComposite(AnsiElement... elements) { - this.elements = Arrays.asList(elements); - } - - /** - * A static builder method - * - * @param elements elements from which create this composite - * @return an instance - */ - public static AnsiComposite of(AnsiElement... elements) { - return new AnsiComposite(elements); - } - - @Override - public String toString() { - return elements.stream() - .map(AnsiElement::toString) - .collect(Collectors.joining(ENCODE_JOIN)); - } -} diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiElement.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiElement.java deleted file mode 100644 index bca5143..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiElement.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -/** - * An ANSI encodable element. - * - * @author Krzysztof Suszynski - * @author Phillip Webb (Spring Boot project) - * @since 0.1.0 - * @see org.springframework.boot.ansi.AnsiElement - */ -public interface AnsiElement { - - /** - * @return the ANSI escape code - */ - @Override - String toString(); - -} diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutput.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutput.java deleted file mode 100644 index f8e9bd2..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutput.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -import javax.annotation.Nullable; -import java.util.Locale; - -import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; - -/** - * Generates ANSI encoded output, automatically attempting to detect if the - * terminal supports ANSI. - * - * @author Krzysztof Suszynski - * @author Phillip Webb (Spring Boot project) - * @since 0.1.0 - * @see org.springframework.boot.ansi.AnsiOutput - */ -public final class AnsiOutput { - - private static Enabled enabled = Enabled.DETECT; - @Nullable - private static Boolean consoleAvailable = true; - @Nullable - private static Boolean ansiCapable; - private static boolean skipWindowsCheck = true; - - private static final String ENCODE_JOIN = ";"; - private static final String ENCODE_START = "\033["; - private static final String ENCODE_END = "m"; - private static final String RESET = "0;" + AnsiColor.DEFAULT; - - private static final String OPERATING_SYSTEM_NAME = System - .getProperty("os.name") - .toLowerCase(Locale.ENGLISH); - - private AnsiOutput() { - // nothing here - } - - /** - * Sets if ANSI output should be skipped on windows os - * - * @param skipWindowsCheck true, if output should be skipped on windows - */ - public static void setSkipWindowsCheck(boolean skipWindowsCheck) { - AnsiOutput.skipWindowsCheck = skipWindowsCheck; - } - - /** - * Sets if ANSI output is enabled. - * - * @param enabled if ANSI is enabled, disabled or detected - */ - public static void setEnabled(Enabled enabled) { - checkNotNull(enabled, "20181220:155934", "Enabled must not be null"); - AnsiOutput.enabled = enabled; - } - - /** - * Sets if the System.console() is known to be available. - * - * @param consoleAvailable if the console is known to be available or - * {@code null} to use standard detection logic. - */ - public static void setConsoleAvailable(@Nullable Boolean consoleAvailable) { - AnsiOutput.consoleAvailable = consoleAvailable; - } - - /** - * Create a new ANSI string from the specified elements. Any - * {@link AnsiElement}s will be encoded as required. - * - * @param elements the elements to encode - * @return a string of the encoded elements - */ - public static String encode(Object... elements) { - StringBuilder sb = new StringBuilder(); - if (isEnabled()) { - buildEnabled(sb, elements); - } else { - buildDisabled(sb, elements); - } - return sb.toString(); - } - - private static void buildEnabled(StringBuilder sb, Object[] elements) { - boolean writingAnsi = false; - boolean containsEncoding = false; - for (Object element : elements) { - if (element instanceof AnsiElement) { - containsEncoding = true; - if (!writingAnsi) { - sb.append(ENCODE_START); - writingAnsi = true; - } else { - sb.append(ENCODE_JOIN); - } - } else { - if (writingAnsi) { - sb.append(ENCODE_END); - writingAnsi = false; - } - } - sb.append(element); - } - if (containsEncoding) { - sb.append(writingAnsi ? ENCODE_JOIN : ENCODE_START); - sb.append(RESET); - sb.append(ENCODE_END); - } - } - - private static void buildDisabled(StringBuilder sb, Object[] elements) { - for (Object element : elements) { - if (!(element instanceof AnsiElement) && element != null) { - sb.append(element); - } - } - } - - private static boolean isEnabled() { - if (enabled == Enabled.DETECT) { - if (ansiCapable == null) { - ansiCapable = detectIfAnsiCapable(); - } - return ansiCapable; - } - return enabled == Enabled.ALWAYS; - } - - private static boolean detectIfAnsiCapable() { - if (Boolean.FALSE.equals(consoleAvailable) - || ((consoleAvailable == null) && (System.console() == null))) { - return false; - } - return skipWindowsCheck || !(OPERATING_SYSTEM_NAME.contains("win")); - } - - /** - * Possible values to pass to {@link AnsiOutput#setEnabled}. Determines when - * to output ANSI escape sequences for coloring application output. - */ - public enum Enabled { - - /** - * Try to detect whether ANSI coloring capabilities are available. The - * default value for {@link AnsiOutput}. - */ - DETECT, - - /** - * Enable ANSI-colored output. - */ - ALWAYS, - - /** - * Disable ANSI-colored output. - */ - NEVER - } -} diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiStyle.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiStyle.java deleted file mode 100644 index bf8676f..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/AnsiStyle.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -/** - * {@link AnsiElement Ansi} styles. - * - * @author Krzysztof Suszynski - * @author Phillip Webb (Spring Boot project) - * @since 0.1.0 - * @see org.springframework.boot.ansi.AnsiStyle - */ -public enum AnsiStyle implements AnsiElement { - - NORMAL("0"), - BOLD("1"), - FAINT("2"), - ITALIC("3"), - UNDERLINE("4"), - BLINK("5"); - - private final String code; - - AnsiStyle(String code) { - this.code = code; - } - - @Override - public String toString() { - return this.code; - } - -} diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java b/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java deleted file mode 100644 index e4def53..0000000 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverter.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.testing.log4j2; - -import io.vavr.collection.HashSet; -import io.vavr.collection.Set; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.layout.PatternLayout; -import org.apache.logging.log4j.core.pattern.ConverterKeys; -import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; -import org.apache.logging.log4j.core.pattern.PatternConverter; -import org.apache.logging.log4j.core.pattern.PatternFormatter; -import org.apache.logging.log4j.core.pattern.PatternParser; -import org.apiguardian.api.API; -import org.apiguardian.api.API.Status; -import pl.wavesoftware.plugs.testing.ansi.AnsiBackground; -import pl.wavesoftware.plugs.testing.ansi.AnsiColor; -import pl.wavesoftware.plugs.testing.ansi.AnsiComposite; -import pl.wavesoftware.plugs.testing.ansi.AnsiElement; -import pl.wavesoftware.plugs.testing.ansi.AnsiOutput; -import pl.wavesoftware.plugs.testing.ansi.AnsiStyle; - -import javax.annotation.Nullable; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Log4j2 {@link LogEventPatternConverter} colors output using the {@link AnsiOutput} - * class. A single option 'styling' can be provided to the converter, or if not specified - * color styling will be picked based on the logging level. - * - * @author Krzysztof Suszynski - * @author Vladimir Tsanev (Spring-Boot project) - * @since 0.1.0 - * @see org.springframework.boot.logging.log4j2.ColorConverter - */ -@Plugin(name = "hightlight-color", category = PatternConverter.CATEGORY) -@ConverterKeys("hl") -public final class HighlightColorConverter extends LogEventPatternConverter { - - private static final Map ELEMENTS; - private static final int NO_ARGS_WITHOUT_STYLE = 1; - private static final int NO_ARGS_WITH_STYLE = 2; - private static final Set VALID_NUMBER_OF_ARGS = HashSet.of( - NO_ARGS_WITHOUT_STYLE, - NO_ARGS_WITH_STYLE - ); - - static { - Map ansiElements = new HashMap<>(); - ansiElements.put("faint", AnsiComposite.of(AnsiColor.WHITE, AnsiStyle.FAINT)); - ansiElements.put("black", AnsiColor.BLACK); - ansiElements.put("red", AnsiColor.RED); - ansiElements.put("green", AnsiColor.GREEN); - ansiElements.put("yellow", AnsiColor.YELLOW); - ansiElements.put("blue", AnsiColor.BLUE); - ansiElements.put("magenta", AnsiColor.MAGENTA); - ansiElements.put("cyan", AnsiColor.CYAN); - ELEMENTS = Collections.unmodifiableMap(ansiElements); - } - - private static final Map LEVELS; - - static { - Map ansiLevels = new HashMap<>(); - ansiLevels.put(Level.FATAL.intLevel(), AnsiComposite.of( - AnsiColor.BLACK, AnsiBackground.RED - )); - ansiLevels.put(Level.ERROR.intLevel(), AnsiComposite.of( - AnsiColor.RED, AnsiStyle.BOLD - )); - ansiLevels.put(Level.WARN.intLevel(), AnsiColor.YELLOW); - ansiLevels.put(Level.INFO.intLevel(), AnsiColor.GREEN); - ansiLevels.put(Level.DEBUG.intLevel(), AnsiColor.BLUE); - ansiLevels.put(Level.TRACE.intLevel(), AnsiColor.CYAN); - LEVELS = Collections.unmodifiableMap(ansiLevels); - } - - private final List formatters; - - @Nullable - private final AnsiElement styling; - - private HighlightColorConverter( - List formatters, - @Nullable AnsiElement styling - ) { - super("style", "style"); - this.formatters = Collections.unmodifiableList(formatters); - this.styling = styling; - } - - /** - * Creates a new instance of the class. Required by Log4j. - * - * @param config the configuration - * @param options the options - * @return a new instance, or {@code null} if the options are invalid - */ - @SuppressWarnings("WeakerAccess") - @API(status = Status.MAINTAINED) - public static HighlightColorConverter newInstance( - Configuration config, - String[] options - ) { - if (!VALID_NUMBER_OF_ARGS.contains(options.length)) { - LOGGER.error("Incorrect number of options on style. " - + "Expected at least 1, received {}", options.length); - return null; - } - if (options[0] == null) { - LOGGER.error("No pattern supplied on style"); - return null; - } - PatternParser parser = PatternLayout.createPatternParser(config); - List formatters = parser.parse(options[0]); - AnsiElement element = null; - if (options.length != 1) { - element = ELEMENTS.get(options[1]); - } - return new HighlightColorConverter(formatters, element); - } - - @Override - public boolean handlesThrowable() { - for (PatternFormatter formatter : this.formatters) { - if (formatter.handlesThrowable()) { - return true; - } - } - return super.handlesThrowable(); - } - - @Override - public void format(LogEvent event, StringBuilder stringBuilder) { - StringBuilder buf = new StringBuilder(); - for (PatternFormatter formatter : this.formatters) { - formatter.format(event, buf); - } - if (buf.length() > 0) { - AnsiElement ansiElement = this.styling; - if (ansiElement == null) { - // Assume highlighting - ansiElement = LEVELS.getOrDefault( - event.getLevel().intLevel(), AnsiColor.BLUE - ); - } - appendAnsiString(stringBuilder, buf.toString(), ansiElement); - } - } - - private static void appendAnsiString( - StringBuilder toAppendTo, - String text, - AnsiElement ansiElement - ) { - toAppendTo.append(AnsiOutput.encode(ansiElement, text)); - } - -} diff --git a/testing/src/main/resources/log4j2-test.xml b/testing/src/main/resources/log4j2-test.xml deleted file mode 100644 index 8306854..0000000 --- a/testing/src/main/resources/log4j2-test.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - %hl{%d{HH:mm:ss.SSS}}{faint} - %hl{[%10.10t]}{yellow} - %hl{%-40.40c{1.}}{cyan} - %hl{:}{faint} %m%n%hl{%xEx} - %hl{%5p} ${DATE} ${THREAD} ${CLASS} ${MSG} - - - - - - - - - - - - - diff --git a/testing/src/test/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutputTest.java b/testing/src/test/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutputTest.java deleted file mode 100644 index bae3f52..0000000 --- a/testing/src/test/java/pl/wavesoftware/plugs/testing/ansi/AnsiOutputTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.testing.ansi; - -import io.vavr.collection.List; -import io.vavr.collection.Seq; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.ArgumentsProvider; -import org.junit.jupiter.params.provider.ArgumentsSource; -import pl.wavesoftware.plugs.testing.ansi.AnsiOutput.Enabled; - -import java.lang.reflect.Field; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.params.provider.Arguments.arguments; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -class AnsiOutputTest { - - @BeforeEach - @AfterEach - void reset() throws NoSuchFieldException, IllegalAccessException { - AnsiOutput.setEnabled(Enabled.DETECT); - AnsiOutput.setConsoleAvailable(true); - AnsiOutput.setSkipWindowsCheck(true); - Field field = AnsiOutput.class.getDeclaredField("ansiCapable"); - field.setAccessible(true); - field.set(null, null); - } - - @Test - void always() { - // given - AnsiOutput.setEnabled(Enabled.ALWAYS); - String text = "Colorful"; - - // when - String encoded = AnsiOutput.encode( - AnsiStyle.ITALIC, - AnsiColor.BLACK, - AnsiBackground.MAGENTA, - text - ); - - // then - assertThat(encoded).isEqualTo("\u001B[3;30;45mColorful\u001B[0;39m"); - } - - @ParameterizedTest - @ArgumentsSource(EncodeArgumentSource.class) - void encode(Seq styling, String expectedResult) { - // given - String text = "Colorful"; - Object[] toBeEncoded = styling.append(text).toJavaArray(); - - // when - String encoded = AnsiOutput.encode(toBeEncoded); - - // then - assertThat(encoded).isEqualTo(expectedResult); - } - - @ParameterizedTest - @ArgumentsSource(EncodeArgumentSource.class) - void encodeOnWindows(Seq styling) { - // given - AnsiOutput.setSkipWindowsCheck(false); - AnsiOutput.setConsoleAvailable(null); - String text = "Non-Color"; - Object[] toBeEncoded = styling.append(text).toJavaArray(); - - // when - String encoded = AnsiOutput.encode(toBeEncoded); - - // then - assertThat(encoded).isEqualTo(text); - } - - private static final class EncodeArgumentSource implements ArgumentsProvider { - - @Override - public Stream provideArguments(ExtensionContext context) { - return Stream.of( - arguments(List.of(AnsiColor.BLUE), "\u001B[34mColorful\u001B[0;39m"), - arguments( - List.of(AnsiColor.BRIGHT_YELLOW, AnsiStyle.BLINK), - "\u001B[93;5mColorful\u001B[0;39m" - ), - arguments( - List.of(AnsiComposite.of( - AnsiStyle.BOLD, - AnsiColor.BLACK, - AnsiBackground.BRIGHT_YELLOW - )), - "\u001B[1;30;103mColorful\u001B[0;39m" - ) - ); - } - } -} diff --git a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorAppender.java b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorAppender.java deleted file mode 100644 index 99a799e..0000000 --- a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorAppender.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.testing.log4j2; - -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginElement; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; -import org.apache.logging.log4j.core.layout.PatternLayout; - -import javax.annotation.Nullable; -import java.io.Serializable; -import java.nio.charset.StandardCharsets; - -/** - * A collector appender - * - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -@Plugin( - name = "Collector", - category = "Core", - elementType = "appender", - printObject = true -) -public final class CollectorAppender extends AbstractAppender { - private final CollectorManager manager; - - private CollectorAppender( - String name, - Layout layout, - @Nullable Filter filter, - CollectorManager manager, - boolean ignoreExceptions - ) { - super(name, filter, layout, ignoreExceptions); - this.manager = manager; - } - - /** - * Factory method for Log4j2 - * - * @param name a name - * @param ignoreExceptions do ignore exceptions - * @param layout a Log4j2 layout like pattern layout - * @param filter a Log4j2 filter - * @return a created instance - */ - @PluginFactory - public static CollectorAppender createAppender( - @Nullable @PluginAttribute("name") String name, - @PluginAttribute("ignoreExceptions") boolean ignoreExceptions, - @Nullable @PluginElement("Layout") Layout layout, - @Nullable @PluginElement("Filters") Filter filter - ) { - - if (name == null) { - LOGGER.error("No name provided for CollectorAppender"); - return null; - } - - CollectorManager manager = CollectorManager.getCollectorManager(name); - if (layout == null) { - layout = PatternLayout.createDefaultLayout(); - } - return new CollectorAppender(name, layout, filter, manager, ignoreExceptions); - } - - @Override - public void append(LogEvent event) { - byte[] bytes = getLayout().toByteArray(event); - String formattedMessage = new String(bytes, StandardCharsets.UTF_8); - manager.collect(event, formattedMessage); - } -} diff --git a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorManager.java b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorManager.java deleted file mode 100644 index 4aeb3ed..0000000 --- a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/CollectorManager.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.testing.log4j2; - -import io.vavr.collection.Traversable; -import org.apache.logging.log4j.core.LogEvent; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * @author Krzysztof Suszynski - * @since 2019-01-05 - */ -final class CollectorManager { - private static final Map MANAGERS = - new HashMap<>(); - - private final List events = new ArrayList<>(); - - static CollectorManager getCollectorManager(String name) { - return MANAGERS.computeIfAbsent(name, ignored -> new CollectorManager()); - } - - Traversable getCollected() { - return io.vavr.collection.List.ofAll(events); - } - - void collect(LogEvent event, String formattedMessage) { - events.add(new CollectedEvent(event, formattedMessage)); - } - - void clear() { - events.clear(); - } - - static final class CollectedEvent { - private final LogEvent event; - private final String formattedMessage; - - CollectedEvent(LogEvent event, String formattedMessage) { - this.event = event; - this.formattedMessage = formattedMessage; - } - - LogEvent getEvent() { - return event; - } - - String getFormattedMessage() { - return formattedMessage; - } - } -} diff --git a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java b/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java deleted file mode 100644 index 8c422d1..0000000 --- a/testing/src/test/java/pl/wavesoftware/plugs/testing/log4j2/HighlightColorConverterTest.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.testing.log4j2; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.status.StatusLogger; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.ArgumentsProvider; -import org.junit.jupiter.params.provider.ArgumentsSource; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import pl.wavesoftware.plugs.testing.log4j2.CollectorManager.CollectedEvent; - -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -@ExtendWith(MockitoExtension.class) -class HighlightColorConverterTest { - - @Nested - class ActualLoggingWithSlf4j { - private static final int EXPECTED_SIZE = 2; - private final CollectorManager manager = - CollectorManager.getCollectorManager("PseudoConsole"); - - @AfterEach - void after() { - manager.clear(); - } - - @Test - void format() { - // given - Logger logger = LoggerFactory.getLogger(HighlightColorConverterTest.class); - - // when - logger.info("info"); - logger.error("error"); - - // then - assertThat(manager.getCollected()) - .hasSize(EXPECTED_SIZE) - .extracting(this::formattedMessageOf) - .containsExactly( - "\u001B[32m INFO\u001B[0;39m \u001B[33m[ main]\u001B[0;39m " + - "\u001B[36mp.w.p.t.l.HighlightColorConverterTest " + - "\u001B[0;39m \u001B[37;2m:\u001B[0;39m info\n", - "\u001B[31;1mERROR\u001B[0;39m \u001B[33m[ main]\u001B[0;39m " + - "\u001B[36mp.w.p.t.l.HighlightColorConverterTest " + - "\u001B[0;39m \u001B[37;2m:\u001B[0;39m error\n" - ); - } - - private String formattedMessageOf(CollectedEvent event) { - return event.getFormattedMessage().replace("\r\n", "\n"); - } - } - - @ExtendWith(MockitoExtension.class) - @Nested - class NewInstance { - - @Mock - private Configuration configuration; - private Level level; - - @BeforeEach - void before() { - level = StatusLogger.getLogger().getLevel(); - StatusLogger.getLogger().setLevel(Level.OFF); - } - - @AfterEach - void after() { - StatusLogger.getLogger().setLevel(level); - Mockito.validateMockitoUsage(); - Mockito.verifyNoMoreInteractions(configuration); - } - - @ParameterizedTest - @ArgumentsSource(NewInstanceArgumentSource.class) - void newInstance(String[] options) { - // when - HighlightColorConverter instance = HighlightColorConverter.newInstance( - configuration, options - ); - - // then - assertThat(instance).isNull(); - } - } - - private static final class NewInstanceArgumentSource implements ArgumentsProvider { - - @Override - public Stream provideArguments(ExtensionContext context) { - return Stream.of( - new Options(new String[0]), - new Options(new String[] { null, "red" }), - new Options(new String[] { "color me", "black", "red"}) - ); - } - } - - private static final class Options implements Arguments { - - private final Object[] value; - - private Options(String[] value) { - this.value = new Object[] { value.clone() }; - } - - @Override - public Object[] get() { - return value.clone(); - } - } - -} diff --git a/testing/src/test/resources/log4j2-test.xml b/testing/src/test/resources/log4j2-test.xml deleted file mode 100644 index b2fbd4e..0000000 --- a/testing/src/test/resources/log4j2-test.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - %hl{%5p} %hl{[%10.10t]}{yellow} %hl{%-40.40c{1.}}{cyan} %hl{:}{faint} %m%n%ex - - - - - ${sys:CONSOLE_LOG_PATTERN} - - - - - - - - - - diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index 8dc3d5e..72113f9 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -125,9 +125,10 @@ - pl.wavesoftware.plugs - testing - ${project.version} + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom test diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java index 451a1a2..006512e 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java @@ -30,12 +30,12 @@ import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; -import pl.wavesoftware.plugs.tools.packager.core.Packager; -import pl.wavesoftware.plugs.tools.packager.core.model.CodeBlock; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.api.Packager; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.api.model.CodeBlock; import javax.inject.Inject; import java.io.File; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java index b551a66..d54e228 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactories.java @@ -19,7 +19,7 @@ import pl.wavesoftware.plugs.tools.maven.plugin.filter.FilterFactory; import pl.wavesoftware.plugs.tools.maven.plugin.io.LoggerFactory; import pl.wavesoftware.plugs.tools.maven.plugin.mapper.PackagerConfigurationFactory; -import pl.wavesoftware.plugs.tools.packager.core.PackagerFactory; +import pl.wavesoftware.plugs.tools.packager.api.PackagerFactory; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java index 10036fc..a18a2c2 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagerFactoriesImpl.java @@ -19,7 +19,7 @@ import pl.wavesoftware.plugs.tools.maven.plugin.filter.FilterFactory; import pl.wavesoftware.plugs.tools.maven.plugin.io.LoggerFactory; import pl.wavesoftware.plugs.tools.maven.plugin.mapper.PackagerConfigurationFactory; -import pl.wavesoftware.plugs.tools.packager.core.PackagerFactory; +import pl.wavesoftware.plugs.tools.packager.api.PackagerFactory; import javax.inject.Inject; import javax.inject.Named; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java index 84c4101..8c31585 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java @@ -20,7 +20,7 @@ import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; -import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; +import pl.wavesoftware.plugs.tools.packager.api.model.FilterableDependency; import java.util.ArrayList; import java.util.Collections; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java index ea81729..bae8fa1 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/ExcludeFilter.java @@ -18,7 +18,7 @@ import org.apache.maven.artifact.Artifact; import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; -import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; +import pl.wavesoftware.plugs.tools.packager.api.model.FilterableDependency; import java.util.List; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java index 6f98294..41620ae 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/FilterFactory.java @@ -18,7 +18,7 @@ import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; import javax.annotation.Nullable; import java.util.List; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java index 1276a0d..ffbbe0c 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/IncludeFilter.java @@ -19,7 +19,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; -import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; +import pl.wavesoftware.plugs.tools.packager.api.model.FilterableDependency; import java.util.List; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java index 93ed40c..b283fe3 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java @@ -21,9 +21,9 @@ import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; import pl.wavesoftware.plugs.tools.maven.plugin.mapper.ArtifactMapper; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java index 7c40283..768add1 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactory.java @@ -20,7 +20,7 @@ import pl.wavesoftware.plugs.tools.maven.plugin.mapper.ArtifactMapper; import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; import javax.annotation.Nullable; import javax.inject.Inject; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java index 14e26c7..be95508 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/package-info.java @@ -19,6 +19,9 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault package pl.wavesoftware.plugs.tools.maven.plugin.filter; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java index 601c749..eef0880 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapper.java @@ -18,7 +18,7 @@ import io.vavr.collection.Traversable; import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java index 59c7d3d..5dfdd0a 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImpl.java @@ -25,7 +25,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.repository.RepositorySystem; import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import javax.inject.Inject; import javax.inject.Named; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java index 9b82496..0eef881 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java @@ -16,9 +16,9 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; -import com.github.zafarkhaja.semver.Version; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; import java.nio.file.Path; @@ -43,8 +43,8 @@ public String name() { } @Override - public Version version() { - return Version.valueOf(getDelegate().getVersion()); + public Semver version() { + return new Semver(getDelegate().getVersion()); } @Override @@ -54,7 +54,7 @@ public Path path() { @Override public ArtifactType type() { - return ArtifactType.fromPackging(getDelegate().getType()); + return ArtifactType.fromPackaging(getDelegate().getType()); } @Override diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java index 6a9cd50..7e4b49c 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenBackedProject.java @@ -23,8 +23,8 @@ import org.apache.maven.model.Dependency; import org.apache.maven.project.MavenProject; import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; import java.nio.file.Path; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java index c21b20f..09c1cea 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenLibrariesFactory.java @@ -19,9 +19,9 @@ import io.vavr.collection.Set; import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.maven.plugin.model.MavenLibraries; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; -import pl.wavesoftware.plugs.tools.packager.core.spi.LibrariesFactory; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.api.spi.LibrariesFactory; import javax.inject.Inject; import javax.inject.Named; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java index 50f131a..54338b8 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java @@ -20,9 +20,9 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; import java.io.File; import java.util.function.Supplier; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java index a276375..c7c2183 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java @@ -19,7 +19,7 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; import javax.inject.Inject; import javax.inject.Named; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java index 1af4faf..c4b438e 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java @@ -17,9 +17,9 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; import io.vavr.Lazy; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; import java.nio.file.Path; import java.util.function.Supplier; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java index 75aaab3..7f5009e 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/PackagerConfigurationFactory.java @@ -19,7 +19,7 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; import java.io.File; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java index 72dfb9e..64abb26 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/package-info.java @@ -19,6 +19,9 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault package pl.wavesoftware.plugs.tools.maven.plugin.mapper; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java index 611ab1e..bbc525a 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java @@ -19,7 +19,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apiguardian.api.API; import org.apiguardian.api.API.Status; -import pl.wavesoftware.plugs.tools.packager.core.model.FilterableDependency; +import pl.wavesoftware.plugs.tools.packager.api.model.FilterableDependency; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java index bc3d97c..3b788ed 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java @@ -23,10 +23,10 @@ import io.vavr.control.Option; import org.apache.maven.artifact.Artifact; import org.slf4j.Logger; -import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; -import pl.wavesoftware.plugs.tools.packager.core.model.Library; -import pl.wavesoftware.plugs.tools.packager.core.model.LibraryCallback; -import pl.wavesoftware.plugs.tools.packager.core.model.LibraryScope; +import pl.wavesoftware.plugs.tools.packager.api.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.model.LibraryCallback; +import pl.wavesoftware.plugs.tools.packager.api.model.LibraryScope; import java.io.IOException; diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java index 9a187d2..ea3c2d1 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/package-info.java @@ -19,6 +19,9 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault package pl.wavesoftware.plugs.tools.maven.plugin.model; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml index 70dab07..19e8513 100644 --- a/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml +++ b/tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml @@ -19,7 +19,7 @@ org.springframework.boot spring-boot-parent - 2.1.3.RELEASE + 2.1.5.RELEASE org.example diff --git a/tools/plugs-packager-api/pom.xml b/tools/plugs-packager-api/pom.xml new file mode 100644 index 0000000..21d6d71 --- /dev/null +++ b/tools/plugs-packager-api/pom.xml @@ -0,0 +1,71 @@ + + + + + 4.0.0 + + plugs-tools-parent + pl.wavesoftware.plugs.tools + 0.1.0-SNAPSHOT + + + plugs-packager-api + Plugs :: Tools :: Packager API + + + false + + + + + pl.wavesoftware.plugs + plug-api + ${project.version} + + + + pl.wavesoftware + eid-exceptions + + + org.slf4j + slf4j-api + + + io.vavr + vavr + + + com.vdurmont + semver4j + + + + + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom + test + + + + diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/Constants.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/Constants.java index 648df57..9461dc0 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Constants.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/Constants.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core; +package pl.wavesoftware.plugs.tools.packager.api; /** * A packager core constants diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Packager.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/Packager.java similarity index 92% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Packager.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/Packager.java index bed49d8..9983b7a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/Packager.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/Packager.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core; +package pl.wavesoftware.plugs.tools.packager.api; -import pl.wavesoftware.plugs.tools.packager.core.model.Or; +import pl.wavesoftware.plugs.tools.packager.api.model.Or; /** *

diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/PackagerFactory.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/PackagerFactory.java similarity index 85% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/PackagerFactory.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/PackagerFactory.java index b20650e..017ccd8 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/PackagerFactory.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/PackagerFactory.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core; +package pl.wavesoftware.plugs.tools.packager.api; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; /** * A packager factory to produce instances of {@link Packager}. diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/RepackagingIsRequiredFactory.java similarity index 80% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/RepackagingIsRequiredFactory.java index 7d86a6e..8b56017 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/RepackagingIsRequiredFactory.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/RepackagingIsRequiredFactory.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core; +package pl.wavesoftware.plugs.tools.packager.api; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackagingIsRequired; /** * A factory of a packaging required condition diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/ProjectDigester.java similarity index 86% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/ProjectDigester.java index d3bc65f..1791c58 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigest.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/ProjectDigester.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.digest; +package pl.wavesoftware.plugs.tools.packager.api.digest; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; import java.io.IOException; @@ -26,7 +26,7 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -public interface ProjectDigest { +public interface ProjectDigester { /** * Digest a project * diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/package-info.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/package-info.java similarity index 81% rename from testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/package-info.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/package-info.java index c5c79f6..9c88444 100644 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/ansi/package-info.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/digest/package-info.java @@ -14,12 +14,10 @@ * limitations under the License. */ -/** - * This package is heavily inspired by Spring Boot implementation. - * - * @see org.springframework.boot.ansi - */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.testing.ansi; +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.api.digest; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEvent.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/ArchiveWriterEvent.java similarity index 93% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEvent.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/ArchiveWriterEvent.java index 2f32a4b..f836e56 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEvent.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/ArchiveWriterEvent.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.jar; +package pl.wavesoftware.plugs.tools.packager.api.jar; /** * An event diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterListener.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/ArchiveWriterListener.java similarity index 94% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterListener.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/ArchiveWriterListener.java index fba4510..b2612b9 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterListener.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/ArchiveWriterListener.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.jar; +package pl.wavesoftware.plugs.tools.packager.api.jar; /** * A listener for specific archive writer event diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesCollector.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollector.java similarity index 90% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesCollector.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollector.java index 09e9c72..3fc6775 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesCollector.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollector.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.jar; +package pl.wavesoftware.plugs.tools.packager.api.jar; import io.vavr.collection.HashSet; import io.vavr.collection.Set; -import pl.wavesoftware.plugs.tools.packager.core.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; /** * A collector of a libraries diff --git a/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/package-info.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/package-info.java similarity index 80% rename from testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/package-info.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/package-info.java index 827aded..0726607 100644 --- a/testing/src/main/java/pl/wavesoftware/plugs/testing/log4j2/package-info.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/jar/package-info.java @@ -14,12 +14,10 @@ * limitations under the License. */ -/** - * This package is heavily inspired by Spring Boot implementation. - * - * @see org.springframework.boot.logging.log4j2 - */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.testing.log4j2; +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.api.jar; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilder.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java similarity index 86% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilder.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java index e174883..3e18d84 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilder.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.manifest; +package pl.wavesoftware.plugs.tools.packager.api.manifest; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; import java.util.jar.JarFile; import java.util.jar.Manifest; diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/package-info.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/package-info.java new file mode 100644 index 0000000..2ce59c1 --- /dev/null +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ReturnTypesAreNonnullByDefault +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.api.manifest; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java similarity index 90% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java index 8c8da27..e791f24 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Artifact.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; -import com.github.zafarkhaja.semver.Version; +import com.vdurmont.semver4j.Semver; import java.nio.file.Path; @@ -40,7 +40,7 @@ public interface Artifact { * * @return a version */ - Version version(); + Semver version(); /** * A path to a file that this artifact represents diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactType.java similarity index 91% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactType.java index dffa2c8..e8ec1b6 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/ArtifactType.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactType.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException; @@ -40,7 +40,7 @@ public String extension() { return extension; } - public static ArtifactType fromPackging(String packaging) { + public static ArtifactType fromPackaging(String packaging) { for (ArtifactType value : ArtifactType.values()) { if (value.extension().equals(packaging)) { return value; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/CodeBlock.java similarity index 92% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/CodeBlock.java index 76ce3d2..354a35f 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/CodeBlock.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/CodeBlock.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; /** * A block of code to be executed diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Filter.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Filter.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java index 41309a2..33b06cf 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Filter.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import io.vavr.collection.Set; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/FilterableDependency.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java similarity index 93% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/FilterableDependency.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java index d7f9a5b..687fb18 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/FilterableDependency.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/IoPossibleBlock.java similarity index 93% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/IoPossibleBlock.java index bb0feb5..db59e98 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleBlock.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/IoPossibleBlock.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import java.io.IOException; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/IoPossibleSupplier.java similarity index 94% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/IoPossibleSupplier.java index 89e2b68..a19f542 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/IoPossibleSupplier.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/IoPossibleSupplier.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import java.io.IOException; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Libraries.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Libraries.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Libraries.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Libraries.java index 6a84af5..129fc2f 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Libraries.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Libraries.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import java.io.IOException; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java similarity index 97% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java index dad32f6..0cf139c 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Library.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import javax.annotation.Nullable; import java.io.File; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryCallback.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/LibraryCallback.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryCallback.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/LibraryCallback.java index bd42d67..b866982 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryCallback.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/LibraryCallback.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import java.io.File; import java.io.IOException; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryScope.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/LibraryScope.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryScope.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/LibraryScope.java index dca7e2c..a333e52 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/LibraryScope.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/LibraryScope.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; /** * The scope of a library. The common {@link #COMPILE}, {@link #RUNTIME} and diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java similarity index 94% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java index 1e8666e..be0369b 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Or.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; /** * This interface represent a block of code that should be execute as a diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/PackagerConfiguration.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/PackagerConfiguration.java index 910980e..a2a186e 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerConfiguration.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/PackagerConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import org.slf4j.Logger; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/PackagerCoordinates.java similarity index 95% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/PackagerCoordinates.java index 84e3270..4b68976 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/PackagerCoordinates.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/PackagerCoordinates.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import java.nio.file.Path; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Project.java similarity index 96% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Project.java index 3b77ab6..dd29dd7 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/Project.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Project.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import io.vavr.collection.Set; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java similarity index 97% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java index 65b8125..02ab155 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackageFailed.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; import org.slf4j.helpers.MessageFormatter; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackagingIsRequired.java similarity index 94% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackagingIsRequired.java index 92ab312..1a818e4 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/RepackagingIsRequired.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackagingIsRequired.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; /** * A interface that represents an entity that can decide if packaging is needed diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/StandardLibraries.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java similarity index 94% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/StandardLibraries.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java index 7634fc4..d48910a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/StandardLibraries.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.model; +package pl.wavesoftware.plugs.tools.packager.api.model; /** * A class that holds list of standard libraries diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/package-info.java similarity index 84% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/package-info.java index 7c2ada4..1a113cb 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/package-info.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/package-info.java @@ -19,6 +19,9 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.tools.packager.core.spi; +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.api.model; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/package-info.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/package-info.java new file mode 100644 index 0000000..6013200 --- /dev/null +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.api; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi/LibrariesFactory.java similarity index 86% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi/LibrariesFactory.java index 18f990c..44fad89 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/spi/LibrariesFactory.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi/LibrariesFactory.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.spi; +package pl.wavesoftware.plugs.tools.packager.api.spi; import io.vavr.collection.Set; import org.slf4j.Logger; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Libraries; /** diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi/package-info.java similarity index 84% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java rename to tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi/package-info.java index af7274b..6bda574 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/model/package-info.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/spi/package-info.java @@ -19,6 +19,9 @@ * @since 0.1.0 */ @ParametersAreNonnullByDefault -package pl.wavesoftware.plugs.tools.packager.core.model; +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.api.spi; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java new file mode 100644 index 0000000..82bbf40 --- /dev/null +++ b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.api.jar; + +import org.junit.jupiter.api.Test; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.model.LibraryScope; + +import java.io.File; + +import static org.assertj.core.api.Assertions.*; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +class LibrariesCollectorTest { + + @Test + void getCollected() { + // given + LibrariesCollector collector = new LibrariesCollector(); + File acmeFile = new File("acme"); + LibraryScope scope = LibraryScope.PROVIDED; + Library library = new Library(acmeFile, scope); + + // when + collector.collect(library); + + // then + assertThat(collector.getCollected()).hasSize(1); + assertThat(collector.getCollected().head()).extracting( + Library::getFile, Library::getScope, Library::getName + ).containsOnly( + acmeFile, scope, "acme" + ); + assertThat(scope.toString()).isEqualTo("provided"); + } +} diff --git a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactTypeTest.java b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactTypeTest.java new file mode 100644 index 0000000..b0fc401 --- /dev/null +++ b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/ArtifactTypeTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.api.model; + +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.Test; +import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException; + +import static org.assertj.core.api.Assertions.*; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +class ArtifactTypeTest { + + @Test + void extension() { + // when + String ext = ArtifactType.RAR.extension(); + + // then + assertThat(ext).isEqualTo("rar"); + } + + @Test + void fromPackaging() { + // when + ArtifactType type = ArtifactType.fromPackaging("jar"); + ThrowingCallable jsThrowingCallable = () -> ArtifactType.fromPackaging("js"); + + // then + assertThat(type).isSameAs(ArtifactType.JAR); + assertThatCode(jsThrowingCallable) + .isInstanceOf(EidIllegalArgumentException.class) + .hasMessageContaining("20190209:000032"); + } +} diff --git a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java new file mode 100644 index 0000000..81b7208 --- /dev/null +++ b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.api.model; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +class RepackageFailedTest { + + @Nested + class Positive { + @Test + void check() { + // when + assertThatCode(() -> + RepackageFailed.check(true).or("polite err") + ).doesNotThrowAnyException(); + } + + @Test + void tring() { + // when + assertThatCode(() -> + RepackageFailed.tring(() -> assertThat(this).isNotNull()) + .or("polite {}", "err") + ).doesNotThrowAnyException(); + } + + @Test + void tringToGet() throws RepackageFailed { + // when + String result = RepackageFailed.tring(() -> "result") + .or(() -> "err"); + + // then + assertThat(result).isEqualTo("result"); + } + } + + @Nested + class Negative { + @Test + void check() { + // when + assertThatCode(() -> + RepackageFailed.check(false).or("err {}", 1) + ).hasMessage("err 1"); + } + + @Test + void tring() { + // when + assertThatCode(() -> + RepackageFailed.tring((IoPossibleBlock) () -> { + throw new IOException("io err"); + }).or(() -> "err 2") + ).hasMessage("err 2"); + } + + @Test + void tringToGet() { + // when + assertThatCode(() -> { + RepackageFailed.tring((IoPossibleSupplier) () -> { + throw new IOException("err io"); + }).or("err 3"); + failBecauseExceptionWasNotThrown(RepackageFailed.class); + }).hasMessage("err 3"); + } + } +} diff --git a/tools/plugs-packager-core/pom.xml b/tools/plugs-packager-core/pom.xml index 188dd09..548dd65 100644 --- a/tools/plugs-packager-core/pom.xml +++ b/tools/plugs-packager-core/pom.xml @@ -36,15 +36,11 @@ - pl.wavesoftware.plugs - plug-api + pl.wavesoftware.plugs.tools + plugs-packager-api ${project.version} - - pl.wavesoftware - eid-exceptions - org.slf4j slf4j-api @@ -53,14 +49,6 @@ javax.inject javax.inject - - io.vavr - vavr - - - com.github.zafarkhaja - java-semver - org.apache.commons commons-compress @@ -68,11 +56,23 @@ - pl.wavesoftware.plugs - testing + pl.wavesoftware.plugs.tools + plugs-packager-samples ${project.version} test + + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom + test + + + org.springframework + spring-test + test + diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java index e7e129a..c33b87a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java @@ -19,22 +19,23 @@ import io.vavr.collection.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pl.wavesoftware.plugs.tools.packager.api.Packager; +import pl.wavesoftware.plugs.tools.packager.api.manifest.ManifestBuilder; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.api.model.Or; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.api.spi.LibrariesFactory; import pl.wavesoftware.plugs.tools.packager.core.jar.JarWriter; -import pl.wavesoftware.plugs.tools.packager.core.jar.LibrariesCollector; +import pl.wavesoftware.plugs.tools.packager.api.jar.LibrariesCollector; import pl.wavesoftware.plugs.tools.packager.core.jar.LibraryHasBeenWritten; import pl.wavesoftware.plugs.tools.packager.core.jar.WritableLibraries; -import pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilder; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; -import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; -import pl.wavesoftware.plugs.tools.packager.core.model.Or; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; -import pl.wavesoftware.plugs.tools.packager.core.spi.LibrariesFactory; import java.io.File; import java.io.IOException; @@ -43,8 +44,8 @@ import java.util.jar.JarFile; import java.util.jar.Manifest; -import static pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed.check; -import static pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed.tring; +import static pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed.check; +import static pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed.tring; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java index 488be1d..90b2879 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackagerFactory.java @@ -16,11 +16,14 @@ package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilder; -import pl.wavesoftware.plugs.tools.packager.core.model.Filter; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; -import pl.wavesoftware.plugs.tools.packager.core.spi.LibrariesFactory; +import pl.wavesoftware.plugs.tools.packager.api.Packager; +import pl.wavesoftware.plugs.tools.packager.api.PackagerFactory; +import pl.wavesoftware.plugs.tools.packager.api.RepackagingIsRequiredFactory; +import pl.wavesoftware.plugs.tools.packager.api.manifest.ManifestBuilder; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.api.spi.LibrariesFactory; import javax.inject.Inject; import javax.inject.Named; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java index cd7f33a..ab2bf0a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequired.java @@ -16,10 +16,10 @@ package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigest; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackagingIsRequired; import java.io.File; import java.io.IOException; @@ -28,7 +28,7 @@ import java.util.jar.Manifest; import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; -import static pl.wavesoftware.plugs.tools.packager.core.Constants.PLUGS_DIGEST_ATTRIBUTE; +import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_DIGEST_ATTRIBUTE; /** * @author Krzysztof Suszynski @@ -38,16 +38,16 @@ final class DefaultRepackagingIsRequired implements RepackagingIsRequired { private final PackagerCoordinates coordinates; private final Project project; - private final ProjectDigest projectDigest; + private final ProjectDigester projectDigester; DefaultRepackagingIsRequired( PackagerCoordinates coordinates, Project project, - ProjectDigest projectDigest + ProjectDigester projectDigester ) { this.coordinates = coordinates; this.project = project; - this.projectDigest = projectDigest; + this.projectDigester = projectDigester; } @Override @@ -63,7 +63,7 @@ private boolean alreadyRepackaged() throws IOException { } try (JarFile jarFile = new JarFile(targetFile)) { Manifest manifest = jarFile.getManifest(); - CharSequence digest = projectDigest.digest(project); + CharSequence digest = projectDigester.digest(project); return manifest != null && digest.equals(manifest.getMainAttributes().getValue(PLUGS_DIGEST_ATTRIBUTE)); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java index f68c319..9b3253a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultRepackagingIsRequiredFactory.java @@ -16,10 +16,11 @@ package pl.wavesoftware.plugs.tools.packager.core; -import pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigest; -import pl.wavesoftware.plugs.tools.packager.core.model.PackagerCoordinates; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackagingIsRequired; +import pl.wavesoftware.plugs.tools.packager.api.RepackagingIsRequiredFactory; +import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; +import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackagingIsRequired; import javax.inject.Inject; import javax.inject.Named; @@ -32,10 +33,10 @@ final class DefaultRepackagingIsRequiredFactory implements RepackagingIsRequiredFactory { - private final ProjectDigest digest; + private final ProjectDigester digest; @Inject - DefaultRepackagingIsRequiredFactory(ProjectDigest digest) { + DefaultRepackagingIsRequiredFactory(ProjectDigester digest) { this.digest = digest; } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java similarity index 58% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImpl.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java index aa0de7b..d9bbfd6 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java @@ -16,7 +16,8 @@ package pl.wavesoftware.plugs.tools.packager.core.digest; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; import javax.inject.Named; import java.io.IOException; @@ -32,17 +33,23 @@ * @since 0.1.0 */ @Named -final class ProjectDigestImpl implements ProjectDigest { +final class ProjectDigesterImpl implements ProjectDigester { @Override public CharSequence digest(Project project) throws IOException { Path sourcePath = project.mainArtifact().path(); Path buildFilePath = project.buildFilePath(); - checkArgument(sourcePath.toFile().isFile(), "20190131:221929"); - CRC32 digest = new CRC32(); - digest.update(sourcePath.toAbsolutePath().toString().getBytes(UTF_8)); - digest.update(Long.toHexString(Files.size(sourcePath)).getBytes(UTF_8)); - digest.update(Long.toHexString(Files.size(buildFilePath)).getBytes(UTF_8)); - digest.update(Long.toHexString(buildFilePath.toFile().lastModified()).getBytes(UTF_8)); - return Long.toHexString(Math.abs(digest.getValue())); + checkArgument(Files.isRegularFile(sourcePath), "20190131:221929"); + CRC32 digester = new CRC32(); + digester.update(sourcePath.toAbsolutePath().toString().getBytes(UTF_8)); + digester.update(Long.toHexString(Files.size(sourcePath)).getBytes(UTF_8)); + digester.update(Long.toHexString(Files.getLastModifiedTime(sourcePath).toMillis()).getBytes(UTF_8)); + digester.update(Long.toHexString(Files.size(buildFilePath)).getBytes(UTF_8)); + digester.update(Long.toHexString(Files.getLastModifiedTime(buildFilePath).toMillis()).getBytes(UTF_8)); + return encode(digester.getValue()); } + + private CharSequence encode(long digest) { + return Long.toUnsignedString(digest, 36); + } + } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/package-info.java new file mode 100644 index 0000000..5849f32 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.core.digest; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java index 22c0444..4295639 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java @@ -16,7 +16,10 @@ package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.tools.packager.core.model.Library; + +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterEvent; +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterListener; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; import javax.annotation.WillClose; import java.io.IOException; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java index bed7483..f91ab3b 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java @@ -22,7 +22,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.helpers.MessageFormatter; -import pl.wavesoftware.plugs.tools.packager.core.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterEvent; +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterListener; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; import javax.annotation.Nullable; import javax.annotation.WillClose; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java index 68c24e1..4fa1105 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibraryHasBeenWritten.java @@ -16,7 +16,8 @@ package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.tools.packager.core.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterEvent; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; /** * @author Krzysztof Suszynski diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java index 361b742..19d9cfa 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java @@ -16,14 +16,14 @@ package pl.wavesoftware.plugs.tools.packager.core.jar; -import pl.wavesoftware.plugs.tools.packager.core.model.Libraries; -import pl.wavesoftware.plugs.tools.packager.core.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.model.Libraries; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; -import static pl.wavesoftware.plugs.tools.packager.core.Constants.LIBRARY_DESTINATION; +import static pl.wavesoftware.plugs.tools.packager.api.Constants.LIBRARY_DESTINATION; import static pl.wavesoftware.plugs.tools.packager.core.jar.FileUtils.isZip; /** diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java index 682c163..a6d6f07 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/package-info.java @@ -18,7 +18,10 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ +@ReturnTypesAreNonnullByDefault @ParametersAreNonnullByDefault package pl.wavesoftware.plugs.tools.packager.core.jar; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java index 3947eaf..edb4e10 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java @@ -19,9 +19,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.wavesoftware.plugs.api.PlugsVersion; -import pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigest; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; -import pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; +import pl.wavesoftware.plugs.tools.packager.api.manifest.ManifestBuilder; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; import javax.inject.Inject; import javax.inject.Named; @@ -30,9 +31,9 @@ import java.util.jar.JarFile; import java.util.jar.Manifest; -import static pl.wavesoftware.plugs.tools.packager.core.Constants.PLUGS_DIGEST_ATTRIBUTE; -import static pl.wavesoftware.plugs.tools.packager.core.Constants.PLUGS_VERSION_ATTRIBUTE; -import static pl.wavesoftware.plugs.tools.packager.core.model.RepackageFailed.tring; +import static pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed.tring; +import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_DIGEST_ATTRIBUTE; +import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_VERSION_ATTRIBUTE; /** * @author Krzysztof Suszynski @@ -44,18 +45,17 @@ final class ManifestBuilderImpl implements ManifestBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(ManifestBuilderImpl.class); - private final ProjectDigest digest; + private final ProjectDigester digest; @Inject - ManifestBuilderImpl(ProjectDigest digest) { + ManifestBuilderImpl(ProjectDigester digest) { this.digest = digest; } @Override - public Manifest buildManifest( - Project project, - JarFile sourceJar - ) throws RepackageFailed { + public Manifest buildManifest(Project project, JarFile sourceJar) + throws RepackageFailed { + Path sourcePath = project.mainArtifact().path(); Manifest manifest = tring(sourceJar::getManifest).or( "Can't read MANIFEST.MF file from source jar: {}", diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java index 937dfe5..70435c4 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/package-info.java @@ -18,7 +18,10 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ +@ReturnTypesAreNonnullByDefault @ParametersAreNonnullByDefault package pl.wavesoftware.plugs.tools.packager.core.manifest; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java index ec2999f..5cdbaf3 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/package-info.java @@ -18,7 +18,10 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ +@ReturnTypesAreNonnullByDefault @ParametersAreNonnullByDefault package pl.wavesoftware.plugs.tools.packager.core; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named b/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named index 4fed7bf..ff25717 100644 --- a/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named +++ b/tools/plugs-packager-core/src/main/resources/META-INF/sisu/javax.inject.Named @@ -1,4 +1,4 @@ -pl.wavesoftware.plugs.tools.packager.core.jar.ProjectDigestImpl +pl.wavesoftware.plugs.tools.packager.core.digest.ProjectDigesterImpl pl.wavesoftware.plugs.tools.packager.core.manifest.ManifestBuilderImpl pl.wavesoftware.plugs.tools.packager.core.DefaultPackagerFactory pl.wavesoftware.plugs.tools.packager.core.DefaultRepackagingIsRequiredFactory diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java deleted file mode 100644 index 170c3aa..0000000 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigestImplTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.core.digest; - -import com.github.zafarkhaja.semver.Version; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import pl.wavesoftware.plugs.tools.packager.core.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.core.model.ArtifactType; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -@ExtendWith(MockitoExtension.class) -class ProjectDigestImplTest { - - @Mock - Project project; - - @Test - void digest() throws IOException, URISyntaxException { - // given - ProjectDigest digester = new ProjectDigestImpl(); - Path buildFilePath = Paths.get(getClass().getResource("pom.xml").toURI()); - Artifact artifact = new Artifact() { - @Override - public String name() { - throw new UnsupportedOperationException("Not yet implemented"); - } - - @Override - public Version version() { - throw new UnsupportedOperationException("Not yet implemented"); - } - - @Override - public Path path() { - throw new UnsupportedOperationException("Not yet implemented"); - } - - @Override - public ArtifactType type() { - throw new UnsupportedOperationException("Not yet implemented"); - } - }; - when(project.buildFilePath()).thenReturn(buildFilePath); - when(project.mainArtifact()).thenReturn(artifact); - - // when - CharSequence digest = digester.digest(project); - - // then - assertThat(digest).isEqualTo("22ssdd33"); - } -} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImplTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImplTest.java new file mode 100644 index 0000000..d9f5c17 --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImplTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.digest; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; +import pl.wavesoftware.sampler.api.SamplerContext; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = PackagerSamplerContext.class) +class ProjectDigesterImplTest { + + @Autowired + private SamplerContext context; + + @Test + void digest() throws IOException { + // given + ProjectDigester digester = new ProjectDigesterImpl(); + Project project = context.get(SimpleProject.class); + + // when + CharSequence digest = digester.digest(project); + + // then + assertThat(digest).isEqualTo("1is34vi"); + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java new file mode 100644 index 0000000..34efffb --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.manifest; + +import com.vdurmont.semver4j.Semver; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; +import pl.wavesoftware.plugs.tools.packager.api.manifest.ManifestBuilder; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; +import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; +import pl.wavesoftware.sampler.api.SamplerContext; + +import java.io.IOException; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_DIGEST_ATTRIBUTE; +import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_VERSION_ATTRIBUTE; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith(MockitoExtension.class) +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = PackagerSamplerContext.class) +class ManifestBuilderImplTest { + + @Mock + private ProjectDigester digester; + + @Mock + private JarFile sourceJar; + + @Autowired + private SamplerContext samplerContext; + + @AfterEach + void after() { + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(digester, sourceJar); + } + + @Test + void buildManifest() throws RepackageFailed, IOException { + // given + Manifest input = new Manifest(); + String hash = "1q2w3e"; + when(digester.digest(any())).thenReturn(hash); + when(sourceJar.getManifest()).thenReturn(input, (Manifest[]) null); + ManifestBuilder builder = new ManifestBuilderImpl(digester); + Project project = samplerContext.get(SimpleProject.class); + + // when + Manifest manifest1 = builder.buildManifest(project, sourceJar); + Manifest manifest2 = builder.buildManifest(project, sourceJar); + + // then + Attributes attributes = manifest1.getMainAttributes(); + assertThat(attributes).isNotNull(); + assertThat(new Semver(attributes.getValue(PLUGS_VERSION_ATTRIBUTE))) + .isGreaterThan(new Semver("0.0.0")); + assertThat(attributes.getValue(PLUGS_DIGEST_ATTRIBUTE)) + .isEqualTo(hash); + + attributes = manifest2.getMainAttributes(); + assertThat(attributes).isNotNull(); + assertThat(new Semver(attributes.getValue(PLUGS_VERSION_ATTRIBUTE))) + .isGreaterThan(new Semver("0.0.0")); + assertThat(attributes.getValue(PLUGS_DIGEST_ATTRIBUTE)) + .isEqualTo(hash); + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java deleted file mode 100644 index 3cace72..0000000 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/SimpleProjectSample.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.core.sample; - -import pl.wavesoftware.plugs.tools.packager.core.model.Project; - -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -final class SimpleProjectSample implements Sample { - @Override - public Project get() { - throw new UnsupportedOperationException("Not yet implemented"); - } -} diff --git a/tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml b/tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml deleted file mode 100644 index 9246447..0000000 --- a/tools/plugs-packager-core/src/test/resources/pl/wavesoftware/plugs/tools/packager/core/sample/pom.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - 4.0.0 - org.example - digest-test - 0.1.0 - diff --git a/tools/plugs-packager-samples/pom.xml b/tools/plugs-packager-samples/pom.xml new file mode 100644 index 0000000..72f8f5e --- /dev/null +++ b/tools/plugs-packager-samples/pom.xml @@ -0,0 +1,69 @@ + + + + + 4.0.0 + + plugs-tools-parent + pl.wavesoftware.plugs.tools + 0.1.0-SNAPSHOT + + + plugs-packager-samples + Plugs :: Tools :: Packager Samples + + + false + + + + + pl.wavesoftware.plugs.tools + plugs-packager-api + ${project.version} + + + pl.wavesoftware.sampler + sampler-spring + 1.0.0 + + + com.google.jimfs + jimfs + 1.1 + + + + + pl.wavesoftware.testing + junit5-starter + 1.0.0 + pom + test + + + org.springframework + spring-test + test + + + + diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java similarity index 59% rename from tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java index 189badb..17dc0fc 100644 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/Sample.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java @@ -14,16 +14,15 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.sample; +package pl.wavesoftware.plugs.tools.packager.sample; -import java.util.function.Supplier; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.ComponentScan.Filter; +import pl.wavesoftware.sampler.spring.Sample; + +@ComponentScan(includeFilters = @Filter(Sample.class)) +@EnableAutoConfiguration +public class PackagerSamplerContext { -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -public interface Sample extends Supplier { - default T getSample() { - return get(); - } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java new file mode 100644 index 0000000..056bec0 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.sampler.api.Sampler; + +public interface HibernateArtifact extends Sampler { + +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java new file mode 100644 index 0000000..5f774d1 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.sampler.api.Sampler; + +public interface Jsr305Artifact extends Sampler { +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java new file mode 100644 index 0000000..d3be6d8 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.sampler.api.Sampler; + +public interface OsgiCoreArtifact extends Sampler { +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java new file mode 100644 index 0000000..48d2553 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.sampler.api.Sampler; + +public interface SimpleProjectBuildArtifact extends Sampler { +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java new file mode 100644 index 0000000..574c241 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +@Sample +final class HibernateArtifactImpl implements HibernateArtifact { + private final SamplerContext context; + + HibernateArtifactImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "hibernate-core", + "org.hibernate", + new Semver("5.4.2.Final") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java new file mode 100644 index 0000000..b5004b9 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +@Sample +final class Jsr305ArtifactImpl implements Jsr305Artifact { + private final SamplerContext context; + + Jsr305ArtifactImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "jsr305", + "com.google.code.findbugs", + new Semver("3.0.2") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java new file mode 100644 index 0000000..8730023 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.sample.fs.VirtualRoot; +import pl.wavesoftware.sampler.api.SamplerContext; + +import java.nio.file.Path; + +final class MavenlikeArtifact implements Artifact { + + private final SamplerContext context; + private final String name; + private final String group; + private final Semver version; + + MavenlikeArtifact( + SamplerContext context, + String name, + String group, + Semver version + ) { + this.context = context; + this.name = name; + this.group = group; + this.version = version; + } + + @Override + public String name() { + return name; + } + + @Override + public Semver version() { + return version; + } + + @Override + public Path path() { + Path root = context.get(VirtualRoot.class); + return root.resolve("home") + .resolve("jenkins") + .resolve(".m2") + .resolve("repository") + .resolve(groupAsPath()) + .resolve(name()) + .resolve(version().toString()) + .resolve(finalName()); + } + + @Override + public ArtifactType type() { + return ArtifactType.JAR; + } + + private String groupAsPath() { + return group.replace('.', '/'); + } + + private String finalName() { + return String.format( + "%s-%s.%s", + name(), version(), type().extension() + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java new file mode 100644 index 0000000..32c383d --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +@Sample +final class OsgiCoreArtifactImpl implements OsgiCoreArtifact { + private final SamplerContext context; + + OsgiCoreArtifactImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "osgi.core", + "org.osgi", + new Semver("7.0.0") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java new file mode 100644 index 0000000..ae02f8d --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.vdurmont.semver4j.Semver; +import io.vavr.Lazy; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; + +@Sample +final class SimpleProjectBuildArtifactImpl implements SimpleProjectBuildArtifact { + + private static final byte[] BYTES = {0x56, 0x45}; + private static final Instant FILE_MOD_TIME = LocalDateTime.of( + 2019, 5, 23, 20, 39 + ).toInstant(ZoneOffset.ofHours(2)); + + private final SamplerContext context; + + SimpleProjectBuildArtifactImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new Artifact() { + + private final Lazy artifactPath = Lazy.of(() -> { + Project project = context.get(SimpleProject.class); + Path path = project.outputPath().resolve(project.finalName()); + tryToExecute(() -> { + Files.createDirectories(path.getParent()); + Files.createFile(path); + Files.write(path, BYTES); + Files.setLastModifiedTime(path, FileTime.from(FILE_MOD_TIME)); + }, "20190523:203722"); + return path; + }); + + @Override + public String name() { + return "simple"; + } + + @Override + public Semver version() { + return new Semver("1.0.0"); + } + + @Override + public Path path() { + return artifactPath.get(); + } + + @Override + public ArtifactType type() { + return ArtifactType.JAR; + } + }; + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java new file mode 100644 index 0000000..5f811dc --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/package-info.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/package-info.java new file mode 100644 index 0000000..38da57e --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java new file mode 100644 index 0000000..1c0bedc --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.fs; + +import pl.wavesoftware.sampler.api.Sampler; + +import java.nio.file.Path; + + +public interface VirtualRoot extends Sampler { + +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java similarity index 52% rename from tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java index 4905d6f..2a32102 100644 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/sample/ProjectSamples.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java @@ -14,28 +14,27 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.core.sample; +package pl.wavesoftware.plugs.tools.packager.sample.fs; -import pl.wavesoftware.plugs.tools.packager.core.model.Project; +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; +import org.springframework.beans.factory.DisposableBean; +import pl.wavesoftware.sampler.spring.Sample; -import java.util.function.Supplier; +import java.nio.file.FileSystem; +import java.nio.file.Path; -/** - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -public enum ProjectSamples implements Sample { - - SIMPLE_PROJECT(new SimpleProjectSample()); +@Sample +final class VirtualRootImpl implements VirtualRoot, DisposableBean { + private final FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); - private final Supplier project; - - ProjectSamples(Supplier project) { - this.project = project; + @Override + public Path create() { + return fs.getPath("/"); } @Override - public Project get() { - return project.get(); + public void destroy() throws Exception { + fs.close(); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/package-info.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/package-info.java new file mode 100644 index 0000000..178ccb6 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.sample.fs; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/package-info.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/package-info.java new file mode 100644 index 0000000..dee81db --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.sample; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/AbstractProject.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/AbstractProject.java new file mode 100644 index 0000000..e930606 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/AbstractProject.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.project; + +import io.vavr.Lazy; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; + +abstract class AbstractProject implements Project { + private static final Instant POM_MOD_TIME = LocalDateTime.of( + 2019, 5, 23, 20, 54 + ).toInstant(ZoneOffset.ofHours(2)); + + private final Path projectRoot; + private final Lazy buildFilePath; + + AbstractProject(Path root) { + this(root, "project"); + } + + AbstractProject(Path root, String projectDir) { + projectRoot = root.resolve("opt").resolve(projectDir); + buildFilePath = Lazy.of(() -> { + Path path = projectRoot.resolve("pom.xml"); + tryToExecute(() -> { + Files.createDirectories(path.getParent()); + Files.createFile(path); + Files.write(path, "".getBytes(StandardCharsets.UTF_8)); + Files.setLastModifiedTime(path, FileTime.from(POM_MOD_TIME)); + }, "20190523:205548"); + return path; + }); + } + + @Override + public Path buildFilePath() { + return buildFilePath.get(); + } + + @Override + public Path outputPath() { + Path path = projectRoot.resolve("target"); + tryToExecute(() -> Files.createDirectories(path), "20190523:205605"); + return path; + } + + @Override + public String finalName() { + return mainArtifact().name() + + '-' + mainArtifact().version() + + '-' + classifier() + + '.' + mainArtifact().type().extension(); + } + + @Override + public String classifier() { + return "plug"; + } + +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java new file mode 100644 index 0000000..4ae8e47 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.project; + +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.sampler.api.Sampler; + +public interface SimpleProject extends Sampler { +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java new file mode 100644 index 0000000..5181933 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.project; + +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.fs.VirtualRoot; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +import java.nio.file.Path; + +@Sample +final class SimpleProjectImpl implements SimpleProject { + + private final SamplerContext context; + + SimpleProjectImpl(SamplerContext context) { + this.context = context; + } + + @Override + public Project create() { + Path root = context.get(VirtualRoot.class); + return new AbstractProject(root, "simple") { + @Override + public Artifact mainArtifact() { + return context.get(SimpleProjectBuildArtifact.class); + } + + @Override + public Set dependencies() { + return HashSet + .of(HibernateArtifact.class, Jsr305Artifact.class) + .map(context::get); + } + + @Override + public Set imports() { + return HashSet + .of(OsgiCoreArtifact.class) + .map(context::get); + } + }; + } + +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/package-info.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/package-info.java new file mode 100644 index 0000000..d41ac66 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +@ParametersAreNonnullByDefault +@ReturnTypesAreNonnullByDefault +package pl.wavesoftware.plugs.tools.packager.sample.project; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContextIT.java b/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContextIT.java new file mode 100644 index 0000000..8a64e15 --- /dev/null +++ b/tools/plugs-packager-samples/src/test/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContextIT.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; +import pl.wavesoftware.sampler.api.SamplerContext; + +import java.nio.file.Path; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = PackagerSamplerContext.class) +class PackagerSamplerContextIT { + + @Autowired + private SamplerContext samplerContext; + + @Test + void get() { + // when + Project project = samplerContext.get(SimpleProject.class); + Path dependency = project.dependencies() + .filter(art -> "hibernate-core".equals(art.name())) + .head() + .path(); + + // then + assertThat(project).isNotNull(); + assertThat(dependency.toString()).isEqualTo( + "/home/jenkins/.m2/repository/org/hibernate/hibernate-core/" + + "5.4.2.Final/hibernate-core-5.4.2.Final.jar" + ); + } + + @Test + void differentScopes() { + // when + Project project1 = samplerContext.get(SimpleProject.class); + Project project2 = samplerContext.get(SimpleProject.class); + UUID firstId = samplerContext.controller().actualId(); + samplerContext.controller().newId(); + Project project3 = samplerContext.get(SimpleProject.class); + Project project4 = samplerContext.get(SimpleProject.class); + UUID secondId = samplerContext.controller().actualId(); + samplerContext.controller().setId(firstId); + Project project5 = samplerContext.get(SimpleProject.class); + Project project6 = samplerContext.get(SimpleProject.class); + project6.dependencies(); + + // then + assertThat(project1).isNotNull().isSameAs(project2); + assertThat(project3).isNotNull().isSameAs(project4); + assertThat(project3).isNotSameAs(project1); + assertThat(project5).isNotNull().isSameAs(project6).isSameAs(project1); + assertThat(firstId).isNotNull().isNotEqualTo(secondId); + assertThat(secondId).isNotNull(); + } + +} diff --git a/tools/pom.xml b/tools/pom.xml index 41313e4..d28e3d4 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -37,7 +37,9 @@ + plugs-packager-api plugs-packager-core + plugs-packager-samples plugs-maven-plugin From 360476703dede45df230ddcf835b8aa5fefaa1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Thu, 29 Aug 2019 22:46:20 +0200 Subject: [PATCH 17/23] Add tools to coverage --- reports/jacoco/pom.xml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/reports/jacoco/pom.xml b/reports/jacoco/pom.xml index 7cceb89..84b8d74 100644 --- a/reports/jacoco/pom.xml +++ b/reports/jacoco/pom.xml @@ -55,6 +55,26 @@ plugs-spring ${project.version} + + pl.wavesoftware.plugs.tools + plugs-maven-plugin + ${project.version} + + + pl.wavesoftware.plugs.tools + plugs-packager-api + ${project.version} + + + pl.wavesoftware.plugs.tools + plugs-packager-core + ${project.version} + + + pl.wavesoftware.plugs.tools + plugs-packager-samples + ${project.version} + From 20ddd7893325b58c347d46a59cc68beb078fd85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Wed, 4 Sep 2019 23:16:56 +0200 Subject: [PATCH 18/23] Fixing Sonar issues --- boms-parent/pom.xml | 4 - examples/pom.xml | 4 - plug-api/pom.xml | 1 - .../wavesoftware/plugs/api/PlugsVersion.java | 4 +- .../wavesoftware/plugs/api/package-info.java | 26 + plugs-core/pom.xml | 1 - .../wavesoftware/plugs/core/package-info.java | 2 + plugs-felix/pom.xml | 1 - plugs-spring/pom.xml | 1 - pom.xml | 7 +- .../tools/maven/plugin/PackagePlugMojo.java | 4 +- .../maven/plugin/filter/MavenFilter.java | 3 +- .../tools/maven/plugin/mapper/MavenInfo.java | 42 ++ .../mapper/MavenPackagerConfiguration.java | 15 +- .../MavenPackagerConfigurationFactory.java | 4 +- .../maven/plugin/io/MavenLoggerTest.java | 478 ++++++++++++++++++ .../api/manifest/ManifestBuilder.java | 2 +- .../tools/packager/api/model/Filter.java | 3 +- .../plugs/tools/packager/api/model/Or.java | 9 +- .../packager/api/model/RepackageFailed.java | 9 +- .../packager/api/model/StandardLibraries.java | 2 +- .../api/model/RepackageFailedTest.java | 2 +- .../tools/packager/core/DefaultPackager.java | 16 +- .../core/digest/ProjectDigesterImpl.java | 13 +- .../packager/core/jar/ArchiveWriter.java | 29 +- .../core/jar/ArchiveWriterEventAware.java | 39 ++ .../core/jar/JarArchiveEntryWriter.java | 107 ++++ .../packager/core/jar/JarEntryWriter.java | 31 ++ .../core/jar/JarFileEntriesWriter.java | 67 +++ .../tools/packager/core/jar/JarWriter.java | 179 +------ .../packager/core/jar/LibrariesJarWriter.java | 102 ++++ .../tools/packager/core/jar/Listeners.java | 60 +++ .../packager/core/jar/WritableLibraries.java | 2 +- .../core/jar/ZipHeaderPeekInputStream.java | 3 +- .../core/manifest/ManifestBuilderImpl.java | 6 +- .../manifest/ManifestBuilderImplTest.java | 3 +- .../sample/PackagerSamplerContext.java | 5 +- .../sample/artifact/impl/SimpleArtifact.java | 81 +++ .../impl/SimpleProjectBuildArtifactImpl.java | 55 +- tools/pom.xml | 4 - 40 files changed, 1121 insertions(+), 305 deletions(-) create mode 100644 plug-api/src/main/java/pl/wavesoftware/plugs/api/package-info.java create mode 100644 tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenInfo.java create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEventAware.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarArchiveEntryWriter.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarEntryWriter.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriter.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java create mode 100644 tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/Listeners.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java diff --git a/boms-parent/pom.xml b/boms-parent/pom.xml index da15604..7bdf30a 100644 --- a/boms-parent/pom.xml +++ b/boms-parent/pom.xml @@ -32,10 +32,6 @@ Plugs :: BOM Parent - - ${project.basedir}/../.. - - maven-plugin-bom diff --git a/examples/pom.xml b/examples/pom.xml index 1dac395..bcd9f80 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -32,8 +32,4 @@ pom Plugs :: Examples Parent - - - ${project.basedir}/../.. - diff --git a/plug-api/pom.xml b/plug-api/pom.xml index 70d0ec7..e6619c0 100644 --- a/plug-api/pom.xml +++ b/plug-api/pom.xml @@ -31,7 +31,6 @@ Plugs :: Plug API - ${project.basedir}/.. false diff --git a/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java b/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java index 96518ca..6ac50db 100644 --- a/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java +++ b/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java @@ -48,8 +48,8 @@ public static String getVersion() { static String manuallyRead() { List urls = Collections.list(tryToExecute( - () -> PlugsVersion.class - .getClassLoader() + () -> Thread.currentThread() + .getContextClassLoader() .getResources("META-INF/MANIFEST.MF"), "20190325:205203" )); diff --git a/plug-api/src/main/java/pl/wavesoftware/plugs/api/package-info.java b/plug-api/src/main/java/pl/wavesoftware/plugs/api/package-info.java new file mode 100644 index 0000000..cfd8347 --- /dev/null +++ b/plug-api/src/main/java/pl/wavesoftware/plugs/api/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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. + */ + +/** + * @author Krzysztof Suszynski + * @since 2019-01-03 + */ +@ReturnTypesAreNonnullByDefault +@ParametersAreNonnullByDefault +package pl.wavesoftware.plugs.api; + +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-core/pom.xml b/plugs-core/pom.xml index 7324aa6..edae12b 100644 --- a/plugs-core/pom.xml +++ b/plugs-core/pom.xml @@ -31,7 +31,6 @@ Plugs :: Plugs Core - ${project.basedir}/.. false diff --git a/plugs-core/src/main/java/pl/wavesoftware/plugs/core/package-info.java b/plugs-core/src/main/java/pl/wavesoftware/plugs/core/package-info.java index d1f589a..f928df0 100644 --- a/plugs-core/src/main/java/pl/wavesoftware/plugs/core/package-info.java +++ b/plugs-core/src/main/java/pl/wavesoftware/plugs/core/package-info.java @@ -18,7 +18,9 @@ * @author Krzysztof Suszynski * @since 2019-01-03 */ +@ReturnTypesAreNonnullByDefault @ParametersAreNonnullByDefault package pl.wavesoftware.plugs.core; +import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugs-felix/pom.xml b/plugs-felix/pom.xml index 76de674..34c6aff 100644 --- a/plugs-felix/pom.xml +++ b/plugs-felix/pom.xml @@ -30,7 +30,6 @@ Plugs :: Felix - ${project.basedir}/.. false diff --git a/plugs-spring/pom.xml b/plugs-spring/pom.xml index 220806f..9ab2895 100644 --- a/plugs-spring/pom.xml +++ b/plugs-spring/pom.xml @@ -31,7 +31,6 @@ Plugs :: Spring Module - ${project.basedir}/.. false diff --git a/pom.xml b/pom.xml index f90ed3d..1909180 100644 --- a/pom.xml +++ b/pom.xml @@ -98,7 +98,6 @@ - ${project.basedir} UTF-8 UTF-8 https://sonarcloud.io/ @@ -123,7 +122,7 @@ org.apiguardian apiguardian-api - 1.0.0 + 1.1.0 org.apache.felix @@ -158,12 +157,12 @@ com.vdurmont semver4j - 2.2.0 + 3.0.0 org.apache.commons commons-compress - 1.18 + 1.19 diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java index 006512e..ab7fb63 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojo.java @@ -219,9 +219,7 @@ private MojoOr repackageAndReport( logger.debug("Building of {} was successful.", coordinates.targetPath()); } catch (RepackageFailed ex) { logger.error( - "Building of {} has failed: {}", - coordinates.targetPath(), - ex.getMessage() + "Building of {} has failed.", coordinates.targetPath() ); throw new MojoExecutionException("Repackage FAILURE!", ex); } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java index b283fe3..fafb5b5 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilter.java @@ -39,8 +39,7 @@ final class MavenFilter implements Filter { } @Override - public Set filterDependencies(Set dependencies) - throws RepackageFailed { + public Set filterDependencies(Set dependencies) { try { return HashSet.ofAll( filters.filter(dependencies.map(artifactMapper::mavenize).toJavaSet()) diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenInfo.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenInfo.java new file mode 100644 index 0000000..a860b09 --- /dev/null +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenInfo.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.project.MavenProject; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class MavenInfo { + private final MavenProject mavenProject; + private final MavenSession mavenSession; + + MavenInfo(MavenProject mavenProject, MavenSession mavenSession) { + this.mavenProject = mavenProject; + this.mavenSession = mavenSession; + } + + MavenProject getMavenProject() { + return mavenProject; + } + + MavenSession getMavenSession() { + return mavenSession; + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java index 54338b8..3f5171c 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfiguration.java @@ -17,8 +17,6 @@ package pl.wavesoftware.plugs.tools.maven.plugin.mapper; import io.vavr.Lazy; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.project.MavenProject; import org.slf4j.Logger; import pl.wavesoftware.plugs.tools.packager.api.model.PackagerConfiguration; import pl.wavesoftware.plugs.tools.packager.api.model.PackagerCoordinates; @@ -35,8 +33,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration { private final ArtifactMapper artifactMapper; private final Logger logger; - private final MavenProject mavenProject; - private final MavenSession mavenSession; + private final MavenInfo mavenInfo; private final Supplier project; private final String classifier; private final boolean attach; @@ -47,8 +44,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration { MavenPackagerConfiguration( ArtifactMapper artifactMapper, Logger logger, - MavenProject project, - MavenSession session, + MavenInfo mavenInfo, String classifier, boolean attach, File outputDirectory, @@ -56,8 +52,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration { ) { this.artifactMapper = artifactMapper; this.logger = logger; - this.mavenProject = project; - this.mavenSession = session; + this.mavenInfo = mavenInfo; this.classifier = classifier; this.attach = attach; this.outputDirectory = outputDirectory; @@ -89,8 +84,8 @@ public Project project() { private Project calculateProject() { return new MavenBackedProject( artifactMapper, - mavenProject, - mavenSession, + mavenInfo.getMavenProject(), + mavenInfo.getMavenSession(), outputDirectory.toPath(), finalName, classifier diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java index c7c2183..0ed5799 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerConfigurationFactory.java @@ -49,11 +49,11 @@ public PackagerConfiguration create( File outputDirectory, String finalName ) { + MavenInfo mavenInfo = new MavenInfo(project, session); return new MavenPackagerConfiguration( artifactMapper, logger, - project, - session, + mavenInfo, classifier, attach, outputDirectory, diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java new file mode 100644 index 0000000..d0f6fcb --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java @@ -0,0 +1,478 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.io; + +import org.apache.maven.plugin.logging.Log; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Krzysztof Suszynski + * @since 2.0.0 + */ +@ExtendWith(MockitoExtension.class) +class MavenLoggerTest { + + private static final String MESSAGE = "msg"; + private static final String FORMAT_ONE = "arg1: {}"; + private static final String FORMAT_TWO = "arg1: {}, arg2: {}"; + private static final String FORMAT_THREE = "arg1: {}, arg2: {}, arg3: {}"; + private static final Object ARG_ONE = 42L; + private static final Object ARG_TWO = 43L; + private static final Object ARG_THREE = 44L; + private static final Throwable THROW = new IOException(); + + @Mock + private Log log; + private Logger logger = new MavenLogger(() -> log); + + @AfterEach + void after() { + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(log); + } + + @Test + void isTraceEnabled() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + boolean result = logger.isTraceEnabled(); + + // then + assertThat(result).isTrue(); + verify(log).isDebugEnabled(); + } + + @Test + void trace() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testTrace() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testTrace1() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testTrace2() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testTrace3() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString(), any()); + } + + @Test + void isDebugEnabled() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + boolean result = logger.isDebugEnabled(); + + // then + assertThat(result).isTrue(); + verify(log).isDebugEnabled(); + } + + @Test + void debug() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testDebug() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testDebug1() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testDebug2() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString()); + } + + @Test + void testDebug3() { + // given + when(log.isDebugEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + verify(log).debug(anyString(), any()); + } + + @Test + void isInfoEnabled() { + // given + when(log.isInfoEnabled()).thenReturn(true); + + // when + boolean result = logger.isInfoEnabled(); + + // then + assertThat(result).isTrue(); + verify(log).isInfoEnabled(); + } + + @Test + void info() { + // given + when(log.isInfoEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.info(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + verify(log).info(anyString()); + } + + @Test + void testInfo() { + // given + when(log.isInfoEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.info(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + verify(log).info(anyString()); + } + + @Test + void testInfo1() { + // given + when(log.isInfoEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.info(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + verify(log).info(anyString()); + } + + @Test + void testInfo2() { + // given + when(log.isInfoEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.info(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + verify(log).info(anyString()); + } + + @Test + void testInfo3() { + // given + when(log.isInfoEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.info(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + verify(log).info(anyString(), any()); + } + + @Test + void isWarnEnabled() { + // given + when(log.isWarnEnabled()).thenReturn(true); + + // when + boolean result = logger.isWarnEnabled(); + + // then + assertThat(result).isTrue(); + verify(log).isWarnEnabled(); + } + + @Test + void warn() { + // given + when(log.isWarnEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + verify(log).warn(anyString()); + } + + @Test + void testWarn() { + // given + when(log.isWarnEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + verify(log).warn(anyString()); + } + + @Test + void testWarn1() { + // given + when(log.isWarnEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + verify(log).warn(anyString()); + } + + @Test + void testWarn2() { + // given + when(log.isWarnEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + verify(log).warn(anyString()); + } + + @Test + void testWarn3() { + // given + when(log.isWarnEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + verify(log).warn(anyString(), any()); + } + + @Test + void isErrorEnabled() { + // given + when(log.isErrorEnabled()).thenReturn(true); + + // when + boolean result = logger.isErrorEnabled(); + + // then + assertThat(result).isTrue(); + verify(log).isErrorEnabled(); + } + + @Test + void error() { + // given + when(log.isErrorEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.error(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + verify(log).error(anyString()); + } + + @Test + void testError() { + // given + when(log.isErrorEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.error(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + verify(log).error(anyString()); + } + + @Test + void testError1() { + // given + when(log.isErrorEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.error(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + verify(log).error(anyString()); + } + + @Test + void testError2() { + // given + when(log.isErrorEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.error(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + verify(log).error(anyString()); + } + + @Test + void testError3() { + // given + when(log.isErrorEnabled()).thenReturn(true); + + // when + ThrowingCallable throwingCallable = () -> logger.error(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + verify(log).error(anyString(), any()); + } +} diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java index 3e18d84..67a27f4 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/manifest/ManifestBuilder.java @@ -38,5 +38,5 @@ public interface ManifestBuilder { * @return a OSGi manifest for a Plug * @throws RepackageFailed if source jar can't be reed */ - Manifest buildManifest(Project project, JarFile sourceJar) throws RepackageFailed; + Manifest buildManifest(Project project, JarFile sourceJar); } diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java index 33b06cf..4e8d449 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Filter.java @@ -32,6 +32,5 @@ public interface Filter { * @return a filtered dependencies * @throws RepackageFailed if filtering cant be done */ - Set filterDependencies(Set dependencies) - throws RepackageFailed; + Set filterDependencies(Set dependencies); } diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java index be0369b..a32d2e5 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Or.java @@ -16,17 +16,20 @@ package pl.wavesoftware.plugs.tools.packager.api.model; +import java.util.function.Consumer; + /** * This interface represent a block of code that should be execute as a * alternative to the basic operation. */ -@FunctionalInterface -public interface Or { +public interface Or extends Consumer { /** * Consumes a block of code * * @param block a block of code * @throws RepackageFailed if repackaging has failed for a some kind of reason */ - void or(CodeBlock block) throws RepackageFailed; + default void or(CodeBlock block) { + accept(block); + } } diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java index 02ab155..e1fe303 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailed.java @@ -27,8 +27,7 @@ * @author Krzysztof Suszynski * @since 0.1.0 */ -@SuppressWarnings("squid:S1162") -public final class RepackageFailed extends Exception { +public final class RepackageFailed extends RuntimeException { private static final long serialVersionUID = 20190208231616L; /** @@ -81,11 +80,11 @@ public static OrElse tring(IoPossibleSupplier supplier) { } public interface OrElse { - T or(Supplier politeErrorSupplier) throws RepackageFailed; - default T or(String politeError) throws RepackageFailed { + T or(Supplier politeErrorSupplier); + default T or(String politeError) { return or(() -> politeError); } - default T or(String politeErrorTemplate, Object... args) throws RepackageFailed { + default T or(String politeErrorTemplate, Object... args) { return or( () -> MessageFormatter .arrayFormat(politeErrorTemplate, args) diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java index d48910a..b979913 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java @@ -27,7 +27,7 @@ public final class StandardLibraries { /** * Represents no libraries. */ - Libraries NONE = callback -> {}; + public static final Libraries NONE = callback -> {}; private StandardLibraries() { // non reachable diff --git a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java index 81b7208..97fa030 100644 --- a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java +++ b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/model/RepackageFailedTest.java @@ -51,7 +51,7 @@ void tring() { } @Test - void tringToGet() throws RepackageFailed { + void tringToGet() { // when String result = RepackageFailed.tring(() -> "result") .or(() -> "err"); diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java index c33b87a..26c76fd 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/DefaultPackager.java @@ -87,7 +87,7 @@ public Or repackage() { }; } - private void doRepackage() throws RepackageFailed { + private void doRepackage() { PackagerCoordinates coordinates = configuration.coordinates(); Artifact source = validateSource(coordinates.sourceArtifact()); Path destination = validateDestination(coordinates.targetPath()); @@ -114,8 +114,7 @@ private static JarFile newJarFile(Artifact source) throws IOException { return new JarFile(source.path().toAbsolutePath().toFile()); } - private static Path validateDestination(Path destination) - throws RepackageFailed { + private static Path validateDestination(Path destination) { File asFile = destination.toFile(); if (asFile.exists()) { check(asFile.isFile()).or( @@ -126,7 +125,7 @@ private static Path validateDestination(Path destination) return destination; } - private static Artifact validateSource(Artifact source) throws RepackageFailed { + private static Artifact validateSource(Artifact source) { check(source.type() == ArtifactType.JAR).or( "Only jar artifacts are supported at this time" ); @@ -140,11 +139,8 @@ private static Artifact validateSource(Artifact source) throws RepackageFailed { } private void repackage( - Project project, - JarFile sourceJar, - Path destination, - Libraries libraries - ) throws RepackageFailed { + Project project, JarFile sourceJar, Path destination, Libraries libraries + ) { tring(() -> Files.deleteIfExists(destination)).or( "Can't remove previously created target file: {}", destination @@ -189,7 +185,7 @@ private void repackage( } } - private static JarWriter newJarWriter(Path destination) throws RepackageFailed { + private static JarWriter newJarWriter(Path destination) { return tring(() -> new JarWriter(destination)).or( "Can't create a jar at destination: {}", destination diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java index d9bbfd6..b1f7952 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/digest/ProjectDigesterImpl.java @@ -34,6 +34,9 @@ */ @Named final class ProjectDigesterImpl implements ProjectDigester { + + private static final int BASE36 = 36; + @Override public CharSequence digest(Project project) throws IOException { Path sourcePath = project.mainArtifact().path(); @@ -42,14 +45,16 @@ public CharSequence digest(Project project) throws IOException { CRC32 digester = new CRC32(); digester.update(sourcePath.toAbsolutePath().toString().getBytes(UTF_8)); digester.update(Long.toHexString(Files.size(sourcePath)).getBytes(UTF_8)); - digester.update(Long.toHexString(Files.getLastModifiedTime(sourcePath).toMillis()).getBytes(UTF_8)); + digester.update(Long.toHexString(Files.getLastModifiedTime(sourcePath).toMillis()) + .getBytes(UTF_8)); digester.update(Long.toHexString(Files.size(buildFilePath)).getBytes(UTF_8)); - digester.update(Long.toHexString(Files.getLastModifiedTime(buildFilePath).toMillis()).getBytes(UTF_8)); + digester.update(Long.toHexString(Files.getLastModifiedTime(buildFilePath).toMillis()) + .getBytes(UTF_8)); return encode(digester.getValue()); } - private CharSequence encode(long digest) { - return Long.toUnsignedString(digest, 36); + private static CharSequence encode(long digest) { + return Long.toUnsignedString(digest, BASE36); } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java index 4295639..dc72465 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriter.java @@ -21,9 +21,7 @@ import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterListener; import pl.wavesoftware.plugs.tools.packager.api.model.Library; -import javax.annotation.WillClose; import java.io.IOException; -import java.io.InputStream; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -31,20 +29,9 @@ * Writer used to write classes into a repackaged JAR. * * @author Krzysztof Suszynski - * @since 0.1.0 + * @since 0.1.0writeNestedLibrary */ interface ArchiveWriter { - /** - * Write a single entry to the JAR. - * - * @param name the name of the entry - * @param inputStream the input stream content - * @throws IOException if the entry cannot be written - */ - void writeEntry( - String name, - @WillClose InputStream inputStream - ) throws IOException; /** * Write the specified manifest. @@ -61,10 +48,8 @@ void writeEntry( * @param library the library * @throws IOException if the write fails */ - void writeNestedLibrary( - String destination, - Library library - ) throws IOException; + void writeLibrary(String destination, Library library) + throws IOException; /** * Write all entries from the specified jar file. @@ -74,13 +59,7 @@ void writeNestedLibrary( */ void writeEntries(JarFile jarFile) throws IOException; - void writeEntries( - JarFile jarFile, - EntryTransformer entryTransformer - ) throws IOException; - void addListener( - Class eventType, - ArchiveWriterListener listener + Class eventType, ArchiveWriterListener listener ); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEventAware.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEventAware.java new file mode 100644 index 0000000..c8369a6 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ArchiveWriterEventAware.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterEvent; +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterListener; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +abstract class ArchiveWriterEventAware implements ArchiveWriter { + private final Listeners listeners = new Listeners(); + + @Override + public void addListener( + Class eventType, ArchiveWriterListener listener + ) { + listeners.addListener(eventType, listener); + } + + Listeners getListeners() { + return listeners; + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarArchiveEntryWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarArchiveEntryWriter.java new file mode 100644 index 0000000..c46e089 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarArchiveEntryWriter.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; +import org.apache.commons.compress.archivers.zip.UnixStat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.util.Collection; +import java.util.function.Supplier; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class JarArchiveEntryWriter { + private static final Logger LOGGER = + LoggerFactory.getLogger(JarArchiveEntryWriter.class); + + private final Context context; + + JarArchiveEntryWriter(Context context) { + this.context = context; + } + + void writeEntry( + JarArchiveEntry entry, @Nullable EntryWriter entryWriter + ) throws IOException { + String parent = entry.getName(); + boolean isDirectory = false; + if (parent.endsWith("/")) { + parent = parent.substring(0, parent.length() - 1); + entry.setUnixMode(UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM); + isDirectory = true; + } else { + entry.setUnixMode(UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM); + } + if (parent.lastIndexOf('/') != -1) { + parent = parent.substring(0, parent.lastIndexOf('/') + 1); + if (!parent.isEmpty()) { + writeEntry(new JarArchiveEntry(parent), null); + } + } + + if (context.register(entry)) { + context.writeHeaderOfEntry(entry); + if (entryWriter != null) { + context.writeEntry(entryWriter); + } + context.finalizeEntry(); + } else { + if (!isDirectory) { + LOGGER.warn( + "Skipping resource, as it was already written: {}", + entry.getName() + ); + } + } + } + + static final class Context { + private final Supplier> registeredEntries; + private final Supplier outputStreamSupplier; + + Context( + Supplier> registeredEntries, + Supplier outputStreamSupplier + ) { + this.registeredEntries = registeredEntries; + this.outputStreamSupplier = outputStreamSupplier; + } + + private boolean register(JarArchiveEntry entry) { + return registeredEntries.get().add(entry.getName()); + } + + private void writeHeaderOfEntry(JarArchiveEntry entry) throws IOException { + outputStreamSupplier.get().putArchiveEntry(entry); + } + + private void writeEntry(EntryWriter entryWriter) throws IOException { + entryWriter.write(outputStreamSupplier.get()); + } + + private void finalizeEntry() throws IOException { + outputStreamSupplier.get().closeArchiveEntry(); + } + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarEntryWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarEntryWriter.java new file mode 100644 index 0000000..6014058 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarEntryWriter.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; + +import javax.annotation.Nullable; +import java.io.IOException; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +interface JarEntryWriter { + void writeEntry(JarArchiveEntry entry, @Nullable EntryWriter entryWriter) + throws IOException; +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriter.java new file mode 100644 index 0000000..a0cd787 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriter.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; + +import java.io.IOException; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.zip.ZipEntry; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class JarFileEntriesWriter { + private final JarArchiveEntryWriter jarArchiveEntryWriter; + + JarFileEntriesWriter(JarArchiveEntryWriter jarArchiveEntryWriter) { + this.jarArchiveEntryWriter = jarArchiveEntryWriter; + } + + void writeEntries( + JarFile jarFile, EntryTransformer entryTransformer + ) throws IOException { + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarArchiveEntry entry = new JarArchiveEntry(entries.nextElement()); + setUpEntry(jarFile, entry); + try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( + jarFile.getInputStream(entry))) { + EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); + JarArchiveEntry transformedEntry = entryTransformer.transform(entry); + if (transformedEntry != null) { + jarArchiveEntryWriter.writeEntry(transformedEntry, entryWriter); + } + } + } + } + + private static void setUpEntry(JarFile jarFile, JarArchiveEntry entry) + throws IOException { + try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( + jarFile.getInputStream(entry))) { + if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) { + new CrcAndSize(inputStream).setupStoredEntry(entry); + } else { + entry.setCompressedSize(-1); + } + } + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java index f91ab3b..d5ace3f 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarWriter.java @@ -18,33 +18,20 @@ import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; -import org.apache.commons.compress.archivers.zip.UnixStat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.helpers.MessageFormatter; -import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterEvent; -import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterListener; import pl.wavesoftware.plugs.tools.packager.api.model.Library; +import pl.wavesoftware.plugs.tools.packager.core.jar.JarArchiveEntryWriter.Context; import javax.annotation.Nullable; -import javax.annotation.WillClose; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.util.Enumeration; import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.Map; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; -import java.util.zip.ZipEntry; /** * Writes JAR content, ensuring valid directory entries are always created and @@ -55,14 +42,12 @@ * @author Andy Wilkinson (Spring Boot project) * @since 0.1.0 */ -public final class JarWriter implements ArchiveWriter, AutoCloseable { - private static final Logger LOGGER = - LoggerFactory.getLogger(JarWriter.class); +public final class JarWriter + extends ArchiveWriterEventAware + implements ArchiveWriter, AutoCloseable { private final JarArchiveOutputStream jarOutput; private final Set writtenEntries = new HashSet<>(); - private final Map, - Set>> listeners = new LinkedHashMap<>(); /** * Create a new {@link JarWriter} instance. @@ -84,34 +69,13 @@ public void writeManifest(Manifest manifest) throws IOException { } @Override - public void writeEntry( - String entryName, - @WillClose InputStream inputStream + public void writeLibrary( + String destination, Library library ) throws IOException { - JarArchiveEntry entry = new JarArchiveEntry(entryName); - writeEntry(entry, new InputStreamEntryWriter(inputStream, true)); - } - - @Override - public void writeNestedLibrary( - String destination, - Library library - ) throws IOException { - File file = library.getFile(); - JarArchiveEntry entry = new JarArchiveEntry(destination + library.getName()); - entry.setTime(getNestedLibraryTime(file)); - new CrcAndSize(file).setupStoredEntry(entry); - writeEntry( - entry, - new InputStreamEntryWriter(new FileInputStream(file), true) + LibrariesJarWriter librariesJarWriter = new LibrariesJarWriter( + this::getListeners, this::writeEntry ); - LibraryHasBeenWritten event = new LibraryHasBeenWritten(library); - for (ArchiveWriterListener listener : listeners.get(LibraryHasBeenWritten.class)) { - @SuppressWarnings("unchecked") - ArchiveWriterListener libraryListener = - (ArchiveWriterListener) listener; - libraryListener.handle(event); - } + librariesJarWriter.writeLibrary(destination, library); } @Override @@ -119,80 +83,6 @@ public void writeEntries(JarFile jarFile) throws IOException { this.writeEntries(jarFile, new SkipManifestMfTransformer()); } - @Override - public void writeEntries( - JarFile jarFile, - EntryTransformer entryTransformer - ) throws IOException { - Enumeration entries = jarFile.entries(); - while (entries.hasMoreElements()) { - JarArchiveEntry entry = new JarArchiveEntry(entries.nextElement()); - setUpEntry(jarFile, entry); - try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( - jarFile.getInputStream(entry))) { - EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); - JarArchiveEntry transformedEntry = entryTransformer.transform(entry); - if (transformedEntry != null) { - writeEntry(transformedEntry, entryWriter); - } - } - } - } - - @Override - public void addListener( - Class eventType, - ArchiveWriterListener listener - ) { - listeners.computeIfAbsent(eventType, k -> new HashSet<>()).add(listener); - } - - private static void setUpEntry(JarFile jarFile, JarArchiveEntry entry) - throws IOException { - try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( - jarFile.getInputStream(entry))) { - if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) { - new CrcAndSize(inputStream).setupStoredEntry(entry); - } else { - entry.setCompressedSize(-1); - } - } - } - - private static long getNestedLibraryTime(File file) { - try { - try (JarFile jarFile = new JarFile(file)) { - Enumeration entries = jarFile.entries(); - Long entry = findTimeOfJarEntries(entries); - if (entry != null) { - return entry; - } - } - } catch (IOException ex) { - // Ignore and just use the source file timestamp - if (LOGGER.isTraceEnabled()) { - LOGGER.trace( - MessageFormatter.format( - "Can't read a supposed JAR file: {}", file - ).toString(), - ex - ); - } - } - return file.lastModified(); - } - - @Nullable - private static Long findTimeOfJarEntries(Enumeration entries) { - while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); - if (!entry.isDirectory()) { - return entry.getTime(); - } - } - return null; - } - /** * Close the writer. * @@ -203,6 +93,19 @@ public void close() throws IOException { this.jarOutput.close(); } + private void writeEntries( + JarFile jarFile, EntryTransformer entryTransformer + ) throws IOException { + Context context = new Context( + () -> writtenEntries, () -> jarOutput + ); + JarArchiveEntryWriter jarArchiveEntryWriter = new JarArchiveEntryWriter(context); + JarFileEntriesWriter jarFileEntriesWriter = new JarFileEntriesWriter( + jarArchiveEntryWriter + ); + jarFileEntriesWriter.writeEntries(jarFile, entryTransformer); + } + /** * Perform the actual write of a {@link JarEntry}. All other write methods delegate to * this one. @@ -212,39 +115,13 @@ public void close() throws IOException { * @throws IOException in case of I/O errors */ private void writeEntry( - JarArchiveEntry entry, - @Nullable EntryWriter entryWriter + JarArchiveEntry entry, @Nullable EntryWriter entryWriter ) throws IOException { - String parent = entry.getName(); - boolean isDirectory = false; - if (parent.endsWith("/")) { - parent = parent.substring(0, parent.length() - 1); - entry.setUnixMode(UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM); - isDirectory = true; - } else { - entry.setUnixMode(UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM); - } - if (parent.lastIndexOf('/') != -1) { - parent = parent.substring(0, parent.lastIndexOf('/') + 1); - if (!parent.isEmpty()) { - writeEntry(new JarArchiveEntry(parent), null); - } - } - - if (this.writtenEntries.add(entry.getName())) { - this.jarOutput.putArchiveEntry(entry); - if (entryWriter != null) { - entryWriter.write(this.jarOutput); - } - this.jarOutput.closeArchiveEntry(); - } else { - if (!isDirectory) { - LOGGER.warn( - "Skipping resource, as it was already written: {}", - entry.getName() - ); - } - } + Context context = new Context( + () -> writtenEntries, () -> jarOutput + ); + JarArchiveEntryWriter jarArchiveEntryWriter = new JarArchiveEntryWriter(context); + jarArchiveEntryWriter.writeEntry(entry, entryWriter); } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java new file mode 100644 index 0000000..0c9a0c1 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.helpers.MessageFormatter; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; + +import javax.annotation.Nullable; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.function.Supplier; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class LibrariesJarWriter { + private static final Logger LOGGER = + LoggerFactory.getLogger(LibrariesJarWriter.class); + + private final Supplier listenersSupplier; + private final JarEntryWriter entryWriter; + + LibrariesJarWriter( + Supplier listenersSupplier, + JarEntryWriter entryWriter + ) { + this.listenersSupplier = listenersSupplier; + this.entryWriter = entryWriter; + } + + void writeLibrary(String destination, Library library) throws IOException { + File file = library.getFile(); + JarArchiveEntry entry = new JarArchiveEntry(destination + library.getName()); + entry.setTime(getNestedLibraryTime(file)); + new CrcAndSize(file).setupStoredEntry(entry); + entryWriter.writeEntry( + entry, + new InputStreamEntryWriter(new FileInputStream(file), true) + ); + LibraryHasBeenWritten event = new LibraryHasBeenWritten(library); + Listeners listeners = listenersSupplier.get(); + listeners.get(LibraryHasBeenWritten.class).forEach( + listener -> listener.handle(event) + ); + } + + private static long getNestedLibraryTime(File file) { + try { + try (JarFile jarFile = new JarFile(file)) { + Enumeration entries = jarFile.entries(); + Long entry = findTimeOfJarEntries(entries); + if (entry != null) { + return entry; + } + } + } catch (IOException ex) { + // Ignore and just use the source file timestamp + if (LOGGER.isTraceEnabled()) { + LOGGER.trace( + MessageFormatter.format( + "Can't read a supposed JAR file: {}", file + ).toString(), + ex + ); + } + } + return file.lastModified(); + } + + @Nullable + private static Long findTimeOfJarEntries(Enumeration entries) { + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + if (!entry.isDirectory()) { + return entry.getTime(); + } + } + return null; + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/Listeners.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/Listeners.java new file mode 100644 index 0000000..87abff9 --- /dev/null +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/Listeners.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterEvent; +import pl.wavesoftware.plugs.tools.packager.api.jar.ArchiveWriterListener; + +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class Listeners { + private final Map, + Set>> map = new LinkedHashMap<>(); + + void addListener( + Class eventType, ArchiveWriterListener listener + ) { + Set> values; + if (!map.containsKey(eventType)) { + values = new LinkedHashSet<>(); + map.put(eventType, values); + } else { + values = map.get(eventType); + } + values.add(listener); + } + + @SuppressWarnings("unchecked") + public Stream> get( + Class eventType + ) { + Set> values = map.get(eventType); + Set> listeners = new LinkedHashSet<>(values.size()); + for (ArchiveWriterListener value : values) { + listeners.add((ArchiveWriterListener) value); + } + return listeners.stream(); + } +} diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java index 19d9cfa..429ec9d 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java @@ -54,7 +54,7 @@ public WritableLibraries(Libraries libraries) throws IOException { public void write(JarWriter writer) throws IOException { for (Map.Entry entry : this.libraryEntryNames.entrySet()) { - writer.writeNestedLibrary( + writer.writeLibrary( entry.getKey().substring(0, entry.getKey().lastIndexOf('/') + 1), entry.getValue() ); diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java index 319d950..2b93d1a 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java @@ -31,6 +31,7 @@ final class ZipHeaderPeekInputStream extends FilterInputStream { private static final byte[] ZIP_HEADER = new byte[]{0x50, 0x4b, 0x03, 0x04}; + private static final int ZIP_HEADER_SIZE = 4; private final byte[] header; private final int headerLength; @@ -40,7 +41,7 @@ final class ZipHeaderPeekInputStream extends FilterInputStream { ZipHeaderPeekInputStream(InputStream in) throws IOException { super(in); - this.header = new byte[4]; + this.header = new byte[ZIP_HEADER_SIZE]; this.headerLength = in.read(this.header); this.headerStream = new ByteArrayInputStream( this.header, diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java index edb4e10..a52adb9 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java @@ -22,7 +22,6 @@ import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; import pl.wavesoftware.plugs.tools.packager.api.manifest.ManifestBuilder; import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; import javax.inject.Inject; import javax.inject.Named; @@ -31,9 +30,9 @@ import java.util.jar.JarFile; import java.util.jar.Manifest; -import static pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed.tring; import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_DIGEST_ATTRIBUTE; import static pl.wavesoftware.plugs.tools.packager.api.Constants.PLUGS_VERSION_ATTRIBUTE; +import static pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed.tring; /** * @author Krzysztof Suszynski @@ -53,8 +52,7 @@ final class ManifestBuilderImpl implements ManifestBuilder { } @Override - public Manifest buildManifest(Project project, JarFile sourceJar) - throws RepackageFailed { + public Manifest buildManifest(Project project, JarFile sourceJar) { Path sourcePath = project.mainArtifact().path(); Manifest manifest = tring(sourceJar::getManifest).or( diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java index 34efffb..0887fc9 100644 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImplTest.java @@ -29,7 +29,6 @@ import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigester; import pl.wavesoftware.plugs.tools.packager.api.manifest.ManifestBuilder; import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.api.model.RepackageFailed; import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext; import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; import pl.wavesoftware.sampler.api.SamplerContext; @@ -70,7 +69,7 @@ void after() { } @Test - void buildManifest() throws RepackageFailed, IOException { + void buildManifest() throws IOException { // given Manifest input = new Manifest(); String hash = "1q2w3e"; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java index 17dc0fc..7703b38 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/PackagerSamplerContext.java @@ -16,13 +16,14 @@ package pl.wavesoftware.plugs.tools.packager.sample; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import pl.wavesoftware.sampler.spring.Sample; +import pl.wavesoftware.sampler.spring.SpringSamplerAutoConfiguration; @ComponentScan(includeFilters = @Filter(Sample.class)) -@EnableAutoConfiguration +@ImportAutoConfiguration(SpringSamplerAutoConfiguration.class) public class PackagerSamplerContext { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java new file mode 100644 index 0000000..15d7f59 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; + +import com.vdurmont.semver4j.Semver; +import io.vavr.Lazy; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; +import pl.wavesoftware.sampler.api.SamplerContext; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; + +/** + * @author Krzysztof Suszynski + * @since 2.0.0 + */ +final class SimpleArtifact implements Artifact { + private static final byte[] BYTES = {0x56, 0x45}; + private static final Instant FILE_MOD_TIME = LocalDateTime.of( + 2019, 5, 23, 20, 39 + ).toInstant(ZoneOffset.ofHours(2)); + + private final Lazy artifactPath; + + SimpleArtifact(SamplerContext context) { + artifactPath = Lazy.of(() -> { + Project project = context.get(SimpleProject.class); + Path path = project.outputPath().resolve(project.finalName()); + tryToExecute(() -> { + Files.createDirectories(path.getParent()); + Files.createFile(path); + Files.write(path, BYTES); + Files.setLastModifiedTime(path, FileTime.from(FILE_MOD_TIME)); + }, "20190523:203722"); + return path; + }); + } + + @Override + public String name() { + return "simple"; + } + + @Override + public Semver version() { + return new Semver("1.0.0"); + } + + @Override + public Path path() { + return artifactPath.get(); + } + + @Override + public ArtifactType type() { + return ArtifactType.JAR; + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java index ae02f8d..743df18 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java @@ -16,33 +16,14 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; -import com.vdurmont.semver4j.Semver; -import io.vavr.Lazy; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; -import pl.wavesoftware.plugs.tools.packager.api.model.Project; import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; -import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; import pl.wavesoftware.sampler.api.SamplerContext; import pl.wavesoftware.sampler.spring.Sample; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.attribute.FileTime; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneOffset; - -import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; - @Sample final class SimpleProjectBuildArtifactImpl implements SimpleProjectBuildArtifact { - private static final byte[] BYTES = {0x56, 0x45}; - private static final Instant FILE_MOD_TIME = LocalDateTime.of( - 2019, 5, 23, 20, 39 - ).toInstant(ZoneOffset.ofHours(2)); - private final SamplerContext context; SimpleProjectBuildArtifactImpl(SamplerContext context) { @@ -51,39 +32,7 @@ final class SimpleProjectBuildArtifactImpl implements SimpleProjectBuildArtifact @Override public Artifact create() { - return new Artifact() { - - private final Lazy artifactPath = Lazy.of(() -> { - Project project = context.get(SimpleProject.class); - Path path = project.outputPath().resolve(project.finalName()); - tryToExecute(() -> { - Files.createDirectories(path.getParent()); - Files.createFile(path); - Files.write(path, BYTES); - Files.setLastModifiedTime(path, FileTime.from(FILE_MOD_TIME)); - }, "20190523:203722"); - return path; - }); - - @Override - public String name() { - return "simple"; - } - - @Override - public Semver version() { - return new Semver("1.0.0"); - } - - @Override - public Path path() { - return artifactPath.get(); - } - - @Override - public ArtifactType type() { - return ArtifactType.JAR; - } - }; + return new SimpleArtifact(context); } + } diff --git a/tools/pom.xml b/tools/pom.xml index d28e3d4..0f0dc80 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -32,10 +32,6 @@ pom Plugs :: Tools Parent - - ${project.basedir}/../.. - - plugs-packager-api plugs-packager-core From 66464ad163ddaeeba693460b0d9e272d9b9c0637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Mon, 23 Sep 2019 00:37:13 +0200 Subject: [PATCH 19/23] Adding test to assert filters --- tools/plugs-maven-plugin/pom.xml | 11 ++ .../filter/AbstractDependencyFilter.java | 9 +- .../maven/plugin/mapper/MavenArtifact.java | 5 + .../model/AbstractFilterableDependency.java | 2 +- .../maven/testing/junit5/MojoFactory.java | 2 + .../maven/testing/junit5/MojoFactoryImpl.java | 6 + .../plugin/filter/MavenFilterFactoryTest.java | 145 ++++++++++++++++++ .../tools/packager/api/model/Artifact.java | 7 + .../api/model/FilterableDependency.java | 4 + ...factImpl.java => AnsiTestingArtifact.java} | 18 ++- ...nateArtifactImpl.java => EidArtifact.java} | 18 ++- .../sample/artifact/HibernateArtifact.java | 20 ++- .../sample/artifact/Jsr305Artifact.java | 21 ++- ...ctImpl.java => Log4j2TestingArtifact.java} | 18 ++- .../{impl => }/MavenlikeArtifact.java | 16 +- .../sample/artifact/OsgiCoreArtifact.java | 21 ++- .../artifact/{impl => }/SimpleArtifact.java | 7 +- .../artifact/SimpleProjectBuildArtifact.java | 17 +- .../artifact/SpringContextArtifact.java | 46 ++++++ .../sample/artifact/SpringCoreArtifact.java | 46 ++++++ ...tifactImpl.java => SpringJclArtifact.java} | 22 ++- .../sample/artifact/impl/package-info.java | 23 --- .../tools/packager/sample/fs/VirtualRoot.java | 17 +- .../packager/sample/fs/VirtualRootImpl.java | 40 ----- .../sample/project/SimpleProject.java | 46 +++++- .../sample/project/SimpleProjectImpl.java | 67 -------- 26 files changed, 484 insertions(+), 170 deletions(-) create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactoryTest.java rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{impl/Jsr305ArtifactImpl.java => AnsiTestingArtifact.java} (70%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{impl/HibernateArtifactImpl.java => EidArtifact.java} (71%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{impl/OsgiCoreArtifactImpl.java => Log4j2TestingArtifact.java} (70%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{impl => }/MavenlikeArtifact.java (86%) rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{impl => }/SimpleArtifact.java (94%) create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringContextArtifact.java create mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringCoreArtifact.java rename tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/{impl/SimpleProjectBuildArtifactImpl.java => SpringJclArtifact.java} (63%) delete mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java delete mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java delete mode 100644 tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java diff --git a/tools/plugs-maven-plugin/pom.xml b/tools/plugs-maven-plugin/pom.xml index 72113f9..76226dc 100644 --- a/tools/plugs-maven-plugin/pom.xml +++ b/tools/plugs-maven-plugin/pom.xml @@ -124,6 +124,12 @@ + + ${project.groupId} + plugs-packager-samples + ${project.version} + test + pl.wavesoftware.testing junit5-starter @@ -131,6 +137,11 @@ pom test + + org.springframework + spring-test + test + org.apache.maven.plugin-testing maven-plugin-testing-harness diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java index 8c31585..91bc690 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/AbstractDependencyFilter.java @@ -22,6 +22,7 @@ import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; import pl.wavesoftware.plugs.tools.packager.api.model.FilterableDependency; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -78,13 +79,15 @@ final boolean equals( if (!dependency.groupId().equals(artifact.getGroupId())) { return false; } - if (!dependency.artifactId().equals(artifact.getArtifactId())) { + @Nullable String dependencyArtifactId = dependency.artifactId(); + if (dependencyArtifactId != null && !dependencyArtifactId.equals(artifact.getArtifactId())) { return false; } - return (dependency.classifier() == null + @Nullable String dependencyClassifier = dependency.classifier(); + return (dependencyClassifier == null || ( artifact.getClassifier() != null - && dependency.classifier().equals(artifact.getClassifier()) + && dependencyClassifier.equals(artifact.getClassifier()) ) ); } diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java index 0eef881..25425c5 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenArtifact.java @@ -42,6 +42,11 @@ public String name() { return getDelegate().getArtifactId(); } + @Override + public String group() { + return getDelegate().getGroupId(); + } + @Override public Semver version() { return new Semver(getDelegate().getVersion()); diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java index bbc525a..40869ba 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/AbstractFilterableDependency.java @@ -42,7 +42,7 @@ abstract class AbstractFilterableDependency implements FilterableDependency { * * @since 0.1.0 */ - @Parameter(required = true) + @Parameter private String artifactId; /** diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java index 95959c1..9d28921 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactory.java @@ -17,6 +17,7 @@ package pl.wavesoftware.maven.testing.junit5; import org.apache.maven.plugin.AbstractMojo; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; /** * @author Krzysztof Suszynski @@ -26,4 +27,5 @@ public interface MojoFactory { MojoFactory configurator(MojoConfigurator configurator); MojoFactory customizer(MavenProjectCustomizer customizer); MojoBuilder builder(Class mojoType); + T lookup(Class role) throws ComponentLookupException; } diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java index 6702abc..c74ec84 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/maven/testing/junit5/MojoFactoryImpl.java @@ -18,6 +18,7 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.testing.MojoRule; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import javax.annotation.Nullable; import java.util.Optional; @@ -56,4 +57,9 @@ public MojoFactory customizer(MavenProjectCustomizer customizer) { public MojoBuilder builder(Class mojoType) { return new MojoBuilderImpl<>(rule, mojoType, configurator); } + + @Override + public T lookup(Class role) throws ComponentLookupException { + return rule.lookup(role); + } } diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactoryTest.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactoryTest.java new file mode 100644 index 0000000..7e28c0d --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/filter/MavenFilterFactoryTest.java @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.filter; + +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import pl.wavesoftware.maven.testing.junit5.MojoExtension; +import pl.wavesoftware.maven.testing.junit5.MojoFactory; +import pl.wavesoftware.plugs.tools.maven.plugin.mapper.ArtifactMapper; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Exclude; +import pl.wavesoftware.plugs.tools.maven.plugin.model.Include; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.Filter; +import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.AnsiTestingArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.EidArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Log4j2TestingArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SpringContextArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SpringCoreArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SpringJclArtifact; +import pl.wavesoftware.sampler.api.SamplerContext; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith({ + MojoExtension.class, + SpringExtension.class +}) +@ContextConfiguration(classes = PackagerSamplerContext.class) +class MavenFilterFactoryTest { + + @Autowired + private SamplerContext samplerContext; + private ArtifactMapper artifactMapper; + + @Test + void create(MojoFactory mojoFactory) throws ComponentLookupException { + // given + artifactMapper = mojoFactory.lookup(ArtifactMapper.class); + MavenFilterFactory filterFactory = new MavenFilterFactory(artifactMapper); + List includes = Arrays.asList( + newInclude("pl.wavesoftware"), + newInclude("pl.wavesoftware.testing"), + newInclude("org.springframework") + ); + List excludes = Arrays.asList( + newExclude("pl.wavesoftware.testing", "ansi"), + newExclude("org.springframework", "spring-jcl") + ); + Set artifacts = HashSet.of( + EidArtifact.class, + Log4j2TestingArtifact.class, + AnsiTestingArtifact.class, + SpringCoreArtifact.class, + SpringContextArtifact.class, + SpringJclArtifact.class, + HibernateArtifact.class, + Jsr305Artifact.class, + OsgiCoreArtifact.class + ).map(samplerContext::get).map(this::mavenize); + Set expected = HashSet.of( + EidArtifact.class, + Log4j2TestingArtifact.class, + SpringCoreArtifact.class, + SpringContextArtifact.class + ).map(samplerContext::get); + + // when + Filter filter = filterFactory.create(includes, excludes); + Set filtered = filter.filterDependencies(artifacts); + + // then + assertThat(filtered).hasSize(4); + assertThat(filtered) + .usingElementComparator(new ArtifactComparator()) + .containsOnlyElementsOf(expected); + } + + private Artifact mavenize(Artifact artifact) { + ArtifactHandler handler = new DefaultArtifactHandlerStub( + artifact.type().extension(), "" + ); + DefaultArtifact maven = new DefaultArtifact( + artifact.group(), artifact.name(), artifact.version().getValue(), + "compile", artifact.type().extension(), null, handler + ); + return artifactMapper.generalize(maven); + } + + private Exclude newExclude(String groupId, String artifactId) { + Exclude exclude = new Exclude(); + exclude.setGroupId(groupId); + exclude.setArtifactId(artifactId); + return exclude; + } + + private Include newInclude(String groupId) { + Include inc = new Include(); + inc.setGroupId(groupId); + return inc; + } + + private static final class ArtifactComparator implements Comparator { + @Override + public int compare(Artifact a1, Artifact a2) { + return a1.group().compareTo(a2.group()) + + a1.name().compareTo(a2.name()) + + a1.version().compareTo(a2.version()); + } + } +} diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java index e791f24..208122f 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Artifact.java @@ -35,6 +35,13 @@ public interface Artifact { */ String name(); + /** + * A group of a artifact + * + * @return a group + */ + String group(); + /** * A version * diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java index 687fb18..de08365 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/FilterableDependency.java @@ -16,12 +16,16 @@ package pl.wavesoftware.plugs.tools.packager.api.model; +import javax.annotation.Nullable; + /** * @author Krzysztof Suszynski * @since 0.1.0 */ public interface FilterableDependency { String groupId(); + @Nullable String artifactId(); + @Nullable String classifier(); } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/AnsiTestingArtifact.java similarity index 70% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/AnsiTestingArtifact.java index b5004b9..8126627 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/Jsr305ArtifactImpl.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/AnsiTestingArtifact.java @@ -14,19 +14,23 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; +package pl.wavesoftware.plugs.tools.packager.sample.artifact; import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; +import pl.wavesoftware.sampler.api.Sampler; import pl.wavesoftware.sampler.api.SamplerContext; import pl.wavesoftware.sampler.spring.Sample; +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ @Sample -final class Jsr305ArtifactImpl implements Jsr305Artifact { +public final class AnsiTestingArtifact implements Sampler { private final SamplerContext context; - Jsr305ArtifactImpl(SamplerContext context) { + AnsiTestingArtifact(SamplerContext context) { this.context = context; } @@ -34,9 +38,9 @@ final class Jsr305ArtifactImpl implements Jsr305Artifact { public Artifact create() { return new MavenlikeArtifact( context, - "jsr305", - "com.google.code.findbugs", - new Semver("3.0.2") + "ansi", + "pl.wavesoftware.testing", + new Semver("1.0.0") ); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/EidArtifact.java similarity index 71% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/EidArtifact.java index 574c241..2a5f93f 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/HibernateArtifactImpl.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/EidArtifact.java @@ -14,19 +14,23 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; +package pl.wavesoftware.plugs.tools.packager.sample.artifact; import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; +import pl.wavesoftware.sampler.api.Sampler; import pl.wavesoftware.sampler.api.SamplerContext; import pl.wavesoftware.sampler.spring.Sample; +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ @Sample -final class HibernateArtifactImpl implements HibernateArtifact { +public final class EidArtifact implements Sampler { private final SamplerContext context; - HibernateArtifactImpl(SamplerContext context) { + EidArtifact(SamplerContext context) { this.context = context; } @@ -34,9 +38,9 @@ final class HibernateArtifactImpl implements HibernateArtifact { public Artifact create() { return new MavenlikeArtifact( context, - "hibernate-core", - "org.hibernate", - new Semver("5.4.2.Final") + "eid-exceptions", + "pl.wavesoftware", + new Semver("2.0.0") ); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java index 056bec0..db426e7 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/HibernateArtifact.java @@ -16,9 +16,27 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; +import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; -public interface HibernateArtifact extends Sampler { +@Sample +public final class HibernateArtifact implements Sampler { + private final SamplerContext context; + HibernateArtifact(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "hibernate-core", + "org.hibernate", + new Semver("5.4.2.Final") + ); + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java index 5f774d1..b5a41a0 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Jsr305Artifact.java @@ -16,8 +16,27 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; +import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; -public interface Jsr305Artifact extends Sampler { +@Sample +public final class Jsr305Artifact implements Sampler { + private final SamplerContext context; + + Jsr305Artifact(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "jsr305", + "com.google.code.findbugs", + new Semver("3.0.2") + ); + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Log4j2TestingArtifact.java similarity index 70% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Log4j2TestingArtifact.java index 32c383d..aa6c5cd 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/OsgiCoreArtifactImpl.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/Log4j2TestingArtifact.java @@ -14,19 +14,23 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; +package pl.wavesoftware.plugs.tools.packager.sample.artifact; import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; +import pl.wavesoftware.sampler.api.Sampler; import pl.wavesoftware.sampler.api.SamplerContext; import pl.wavesoftware.sampler.spring.Sample; +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ @Sample -final class OsgiCoreArtifactImpl implements OsgiCoreArtifact { +public final class Log4j2TestingArtifact implements Sampler { private final SamplerContext context; - OsgiCoreArtifactImpl(SamplerContext context) { + Log4j2TestingArtifact(SamplerContext context) { this.context = context; } @@ -34,9 +38,9 @@ final class OsgiCoreArtifactImpl implements OsgiCoreArtifact { public Artifact create() { return new MavenlikeArtifact( context, - "osgi.core", - "org.osgi", - new Semver("7.0.0") + "log4j2", + "pl.wavesoftware.testing", + new Semver("1.0.0") ); } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java similarity index 86% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java index 8730023..f5d2cfe 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/MavenlikeArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; +package pl.wavesoftware.plugs.tools.packager.sample.artifact; import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; @@ -48,6 +48,11 @@ public String name() { return name; } + @Override + public String group() { + return group; + } + @Override public Semver version() { return version; @@ -81,4 +86,13 @@ private String finalName() { name(), version(), type().extension() ); } + + @Override + public String toString() { + return "MavenlikeArtifact{" + + "name='" + name + '\'' + + ", group='" + group + '\'' + + ", version=" + version + + '}'; + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java index d3be6d8..ad5edec 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/OsgiCoreArtifact.java @@ -16,8 +16,27 @@ package pl.wavesoftware.plugs.tools.packager.sample.artifact; +import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; -public interface OsgiCoreArtifact extends Sampler { +@Sample +public final class OsgiCoreArtifact implements Sampler { + private final SamplerContext context; + + OsgiCoreArtifact(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "osgi.core", + "org.osgi", + new Semver("7.0.0") + ); + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleArtifact.java similarity index 94% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleArtifact.java index 15d7f59..a3d2445 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleArtifact.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; +package pl.wavesoftware.plugs.tools.packager.sample.artifact; import com.vdurmont.semver4j.Semver; import io.vavr.Lazy; @@ -64,6 +64,11 @@ public String name() { return "simple"; } + @Override + public String group() { + return "org.example"; + } + @Override public Semver version() { return new Semver("1.0.0"); diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java index 48d2553..f984df7 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SimpleProjectBuildArtifact.java @@ -18,6 +18,21 @@ import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +@Sample +public final class SimpleProjectBuildArtifact implements Sampler { + + private final SamplerContext context; + + SimpleProjectBuildArtifact(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new SimpleArtifact(context); + } -public interface SimpleProjectBuildArtifact extends Sampler { } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringContextArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringContextArtifact.java new file mode 100644 index 0000000..233389e --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringContextArtifact.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Sample +public final class SpringContextArtifact implements Sampler { + private final SamplerContext context; + + SpringContextArtifact(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "spring-context", + "org.springframework", + new Semver("5.1.9.RELEASE") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringCoreArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringCoreArtifact.java new file mode 100644 index 0000000..93dc527 --- /dev/null +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringCoreArtifact.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.sample.artifact; + +import com.vdurmont.semver4j.Semver; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@Sample +public final class SpringCoreArtifact implements Sampler { + private final SamplerContext context; + + SpringCoreArtifact(SamplerContext context) { + this.context = context; + } + + @Override + public Artifact create() { + return new MavenlikeArtifact( + context, + "spring-core", + "org.springframework", + new Semver("5.1.9.RELEASE") + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringJclArtifact.java similarity index 63% rename from tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java rename to tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringJclArtifact.java index 743df18..dc6faae 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/SimpleProjectBuildArtifactImpl.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/SpringJclArtifact.java @@ -14,25 +14,33 @@ * limitations under the License. */ -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; +package pl.wavesoftware.plugs.tools.packager.sample.artifact; +import com.vdurmont.semver4j.Semver; import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; +import pl.wavesoftware.sampler.api.Sampler; import pl.wavesoftware.sampler.api.SamplerContext; import pl.wavesoftware.sampler.spring.Sample; +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ @Sample -final class SimpleProjectBuildArtifactImpl implements SimpleProjectBuildArtifact { - +public final class SpringJclArtifact implements Sampler { private final SamplerContext context; - SimpleProjectBuildArtifactImpl(SamplerContext context) { + SpringJclArtifact(SamplerContext context) { this.context = context; } @Override public Artifact create() { - return new SimpleArtifact(context); + return new MavenlikeArtifact( + context, + "spring-jcl", + "org.springframework", + new Semver("5.1.9.RELEASE") + ); } - } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java deleted file mode 100644 index 5f811dc..0000000 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/impl/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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. - */ - -@ParametersAreNonnullByDefault -@ReturnTypesAreNonnullByDefault -package pl.wavesoftware.plugs.tools.packager.sample.artifact.impl; - -import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java index 1c0bedc..efc9afd 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRoot.java @@ -16,11 +16,26 @@ package pl.wavesoftware.plugs.tools.packager.sample.fs; +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; +import org.springframework.beans.factory.DisposableBean; import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.spring.Sample; +import java.nio.file.FileSystem; import java.nio.file.Path; +@Sample +public final class VirtualRoot implements Sampler, DisposableBean { + private final FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); -public interface VirtualRoot extends Sampler { + @Override + public Path create() { + return fs.getPath("/"); + } + @Override + public void destroy() throws Exception { + fs.close(); + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java deleted file mode 100644 index 2a32102..0000000 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/fs/VirtualRootImpl.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.sample.fs; - -import com.google.common.jimfs.Configuration; -import com.google.common.jimfs.Jimfs; -import org.springframework.beans.factory.DisposableBean; -import pl.wavesoftware.sampler.spring.Sample; - -import java.nio.file.FileSystem; -import java.nio.file.Path; - -@Sample -final class VirtualRootImpl implements VirtualRoot, DisposableBean { - private final FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); - - @Override - public Path create() { - return fs.getPath("/"); - } - - @Override - public void destroy() throws Exception { - fs.close(); - } -} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java index 4ae8e47..a17ef32 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProject.java @@ -16,8 +16,52 @@ package pl.wavesoftware.plugs.tools.packager.sample.project; +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; import pl.wavesoftware.plugs.tools.packager.api.model.Project; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; +import pl.wavesoftware.plugs.tools.packager.sample.fs.VirtualRoot; import pl.wavesoftware.sampler.api.Sampler; +import pl.wavesoftware.sampler.api.SamplerContext; +import pl.wavesoftware.sampler.spring.Sample; -public interface SimpleProject extends Sampler { +import java.nio.file.Path; + +@Sample +public final class SimpleProject implements Sampler { + + private final SamplerContext context; + + SimpleProject(SamplerContext context) { + this.context = context; + } + + @Override + public Project create() { + Path root = context.get(VirtualRoot.class); + return new AbstractProject(root, "simple") { + @Override + public Artifact mainArtifact() { + return context.get(SimpleProjectBuildArtifact.class); + } + + @Override + public Set dependencies() { + return HashSet + .of(HibernateArtifact.class, Jsr305Artifact.class) + .map(context::get); + } + + @Override + public Set imports() { + return HashSet + .of(OsgiCoreArtifact.class) + .map(context::get); + } + }; + } } diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java deleted file mode 100644 index 5181933..0000000 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/project/SimpleProjectImpl.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.sample.project; - -import io.vavr.collection.HashSet; -import io.vavr.collection.Set; -import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; -import pl.wavesoftware.plugs.tools.packager.api.model.Project; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.HibernateArtifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.Jsr305Artifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.OsgiCoreArtifact; -import pl.wavesoftware.plugs.tools.packager.sample.artifact.SimpleProjectBuildArtifact; -import pl.wavesoftware.plugs.tools.packager.sample.fs.VirtualRoot; -import pl.wavesoftware.sampler.api.SamplerContext; -import pl.wavesoftware.sampler.spring.Sample; - -import java.nio.file.Path; - -@Sample -final class SimpleProjectImpl implements SimpleProject { - - private final SamplerContext context; - - SimpleProjectImpl(SamplerContext context) { - this.context = context; - } - - @Override - public Project create() { - Path root = context.get(VirtualRoot.class); - return new AbstractProject(root, "simple") { - @Override - public Artifact mainArtifact() { - return context.get(SimpleProjectBuildArtifact.class); - } - - @Override - public Set dependencies() { - return HashSet - .of(HibernateArtifact.class, Jsr305Artifact.class) - .map(context::get); - } - - @Override - public Set imports() { - return HashSet - .of(OsgiCoreArtifact.class) - .map(context::get); - } - }; - } - -} From 213121805f9912f36f1d951c981b6bf5883fcd76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Tue, 24 Sep 2019 16:24:58 +0200 Subject: [PATCH 20/23] Adding couple more tests to cover code --- .../tools/packager/core/jar/CrcAndSize.java | 5 +- .../jar/{FileUtils.java => IsZipFile.java} | 35 +++--- .../packager/core/jar/WritableLibraries.java | 4 +- .../core/jar/ZipHeaderPeekInputStream.java | 2 +- .../packager/core/jar/IsZipFileTest.java | 106 ++++++++++++++++++ .../jar/ZipHeaderPeekInputStreamTest.java | 48 ++++++++ .../src/test/resources/example.zip | Bin 0 -> 179 bytes 7 files changed, 181 insertions(+), 19 deletions(-) rename tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/{FileUtils.java => IsZipFile.java} (66%) create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFileTest.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStreamTest.java create mode 100644 tools/plugs-packager-core/src/test/resources/example.zip diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java index 8e276f3..d1b5e5b 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java @@ -18,6 +18,7 @@ import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import javax.annotation.WillNotClose; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -44,7 +45,7 @@ final class CrcAndSize { } } - CrcAndSize(InputStream inputStream) throws IOException { + CrcAndSize(@WillNotClose InputStream inputStream) throws IOException { load(inputStream); } @@ -55,7 +56,7 @@ void setupStoredEntry(JarArchiveEntry entry) { entry.setMethod(ZipEntry.STORED); } - private void load(InputStream inputStream) throws IOException { + private void load(@WillNotClose InputStream inputStream) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFile.java similarity index 66% rename from tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java rename to tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFile.java index b92c19a..f6e3fc0 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/FileUtils.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFile.java @@ -19,36 +19,44 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.BooleanSupplier; /** * Utilities for manipulating files and directories in Spring Boot tooling. * * @author Krzysztof Suszynski - * @author Dave Syer (Spring Boot project) - * @author Phillip Webb (Spring Boot project) * @since 0.1.0 */ -final class FileUtils { +final class IsZipFile implements BooleanSupplier { - private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class); + private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(IsZipFile.class); private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 }; - private FileUtils() { - // non reachable + private final Path path; + private final Logger logger; + + IsZipFile(Path path) { + this(path, DEFAULT_LOGGER); + } + + IsZipFile(Path path, Logger logger) { + this.path = path; + this.logger = logger; } - static boolean isZip(File file) { + @Override + public boolean getAsBoolean() { try { - try (FileInputStream fileInputStream = new FileInputStream(file)) { - return isZip(fileInputStream); + try (InputStream inputStream = Files.newInputStream(path)) { + return isZip(inputStream); } } catch (IOException ex) { - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("Can't read file: " + file, ex); + if (logger.isTraceEnabled()) { + logger.trace("Can't read file: " + path, ex); } return false; } @@ -62,5 +70,4 @@ private static boolean isZip(InputStream inputStream) throws IOException { } return true; } - } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java index 429ec9d..a0a54b9 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java @@ -24,7 +24,6 @@ import java.util.Map; import static pl.wavesoftware.plugs.tools.packager.api.Constants.LIBRARY_DESTINATION; -import static pl.wavesoftware.plugs.tools.packager.core.jar.FileUtils.isZip; /** * A libraries that can be written @@ -38,7 +37,8 @@ public final class WritableLibraries { public WritableLibraries(Libraries libraries) throws IOException { libraries.doWithLibraries(library -> { - if (isZip(library.getFile())) { + IsZipFile isZipFile = new IsZipFile(library.getFile().toPath()); + if (isZipFile.getAsBoolean()) { Library existing = this.libraryEntryNames.putIfAbsent( LIBRARY_DESTINATION + library.getName(), library diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java index 2b93d1a..4ba65b7 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStream.java @@ -31,7 +31,7 @@ final class ZipHeaderPeekInputStream extends FilterInputStream { private static final byte[] ZIP_HEADER = new byte[]{0x50, 0x4b, 0x03, 0x04}; - private static final int ZIP_HEADER_SIZE = 4; + private static final int ZIP_HEADER_SIZE = ZIP_HEADER.length; private final byte[] header; private final int headerLength; diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFileTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFileTest.java new file mode 100644 index 0000000..882a441 --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/IsZipFileTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.matches; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class IsZipFileTest { + + @Nested + @ExtendWith(MockitoExtension.class) + class NonExistingFile { + @Mock + private Logger logger; + + private Path tempDir = Paths.get(System.getProperty("java.io.tmpdir")) + .resolve("file-utils-test"); + private Path path = tempDir.resolve(UUID.randomUUID().toString()); + + @AfterEach + void after() { + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(logger); + } + + @Test + void withDefaultConstructor() { + // given + IsZipFile isZipFile = new IsZipFile(path); + + // when + boolean result = isZipFile.getAsBoolean(); + + // then + assertThat(result).isFalse(); + } + + @Test + void withPassingLogger() { + // given + when(logger.isTraceEnabled()).thenReturn(true); + IsZipFile isZipFile = new IsZipFile(path, logger); + + // when + boolean result = isZipFile.getAsBoolean(); + + // then + assertThat(result).isFalse(); + verify(logger).isTraceEnabled(); + verify(logger).trace(matches("^Can't read file.*"), + any(IOException.class)); + } + } + + @Nested + class NonZipFile { + @Test + void checkTestFileForBeingZip() { + // given + Path path = Paths.get( + getClass() + .getResource(IsZipFileTest.class.getSimpleName() + ".class") + .getFile() + .replaceFirst("/([a-zA-Z]):/", "$1:/") + ); + IsZipFile isZipFile = new IsZipFile(path); + + // when + boolean result = isZipFile.getAsBoolean(); + + // then + assertThat(result).isFalse(); + } + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStreamTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStreamTest.java new file mode 100644 index 0000000..b72e82d --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/ZipHeaderPeekInputStreamTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.CRC32; + +import static org.assertj.core.api.Assertions.assertThat; + +class ZipHeaderPeekInputStreamTest { + + @Test + void read() throws IOException { + // given + CRC32 crc32 = new CRC32(); + try (InputStream inputStream = getClass().getClassLoader() + .getResourceAsStream("example.zip"); + ZipHeaderPeekInputStream zipHeaderPeekInputStream = + new ZipHeaderPeekInputStream(inputStream) + ) { + // when + int next; + while ((next = zipHeaderPeekInputStream.read()) != -1) { + crc32.update(next); + } + } + + // then + assertThat(crc32.getValue()).isEqualTo(3245340136L); + } +} diff --git a/tools/plugs-packager-core/src/test/resources/example.zip b/tools/plugs-packager-core/src/test/resources/example.zip new file mode 100644 index 0000000000000000000000000000000000000000..f75a1993fb4f0ca4b4e728e31f486bc5463674c9 GIT binary patch literal 179 zcmWIWW@Zs(U|`^6;BK<;Pvwu8C=KK(fkhY?f?OS4d|mZ&Q@D#tOFou{dYnGv`Sg-2 zgPz{=KYJ!!*)u6gk0HRDk;$F`ml-NRlR!WLOd^@c1r=gskYKoU>i^ Date: Tue, 24 Sep 2019 22:33:43 +0200 Subject: [PATCH 21/23] Tests to check version and maven logger classes --- .../wavesoftware/plugs/api/PlugsVersion.java | 23 +- .../plugs/api/PlugsVersionIT.java | 4 +- .../plugs/api/PlugsVersionTest.java | 38 + .../maven/plugin/model/MavenLibraries.java | 2 +- .../maven/plugin/io/MavenLoggerTest.java | 896 ++++++++++-------- .../tools/packager/api/model/Library.java | 22 +- .../api/jar/LibrariesCollectorTest.java | 13 +- .../tools/packager/core/jar/CrcAndSize.java | 8 +- .../packager/core/jar/LibrariesJarWriter.java | 40 +- .../packager/core/jar/WritableLibraries.java | 2 +- .../core/manifest/ManifestBuilderImpl.java | 2 +- .../core/jar/LibrariesJarWriterTest.java | 81 ++ .../sample/artifact/MavenlikeArtifact.java | 9 - 13 files changed, 674 insertions(+), 466 deletions(-) create mode 100644 plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionTest.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java diff --git a/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java b/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java index 6ac50db..6568348 100644 --- a/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java +++ b/plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java @@ -27,33 +27,40 @@ import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; -import static pl.wavesoftware.eid.utils.EidPreconditions.checkState; public final class PlugsVersion { + private final ClassLoader classLoader; + private PlugsVersion() { - // not reachable + this(Thread.currentThread().getContextClassLoader()); + } + + PlugsVersion(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + public static PlugsVersion get() { + return new PlugsVersion(); } - public static String getVersion() { + public String getVersion() { Optional maybeVersion = Optional.ofNullable( PlugsVersion.class.getPackage().getImplementationVersion() ); return checkNotNull( - maybeVersion.orElseGet(PlugsVersion::manuallyRead), + maybeVersion.orElseGet(this::manuallyRead), "20190325:202509" ); } - static String manuallyRead() { + String manuallyRead() { List urls = Collections.list(tryToExecute( - () -> Thread.currentThread() - .getContextClassLoader() + () -> classLoader .getResources("META-INF/MANIFEST.MF"), "20190325:205203" )); - checkState(!urls.isEmpty(), "20190325:205809"); URL location = PlugsVersion.class.getProtectionDomain().getCodeSource().getLocation(); URL resource = urls.stream() .filter(url -> url.toString().contains(location.toString())) diff --git a/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java index 85c7bf5..a2e6c5b 100644 --- a/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java +++ b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionIT.java @@ -27,14 +27,14 @@ class PlugsVersionIT { @Test void getVersion() { - Semver version = new Semver(PlugsVersion.getVersion()); + Semver version = new Semver(PlugsVersion.get().getVersion()); assertThat(version.isGreaterThanOrEqualTo(BASE)).isTrue(); } @Test void manually() { - Semver version = new Semver(PlugsVersion.manuallyRead()); + Semver version = new Semver(PlugsVersion.get().manuallyRead()); assertThat(version.isGreaterThanOrEqualTo(BASE)).isTrue(); } diff --git a/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionTest.java b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionTest.java new file mode 100644 index 0000000..62fb8fd --- /dev/null +++ b/plug-api/src/test/java/pl/wavesoftware/plugs/api/PlugsVersionTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.api; + +import org.junit.jupiter.api.Test; + +import java.net.URL; +import java.net.URLClassLoader; + +import static org.assertj.core.api.Assertions.assertThatCode; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +final class PlugsVersionTest { + private ClassLoader classLoader = new URLClassLoader(new URL[0], null); + + @Test + void getVersion() { + assertThatCode(() -> new PlugsVersion(classLoader).getVersion()) + .hasMessageContaining("20190325:205648"); + } +} diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java index 3b788ed..9051a8b 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/model/MavenLibraries.java @@ -74,7 +74,7 @@ public void doWithLibraries(LibraryCallback callback) throws IOException { } callback.library(new Library( name.toString(), - artifact.getFile(), + artifact.getFile().toPath(), scopeOption.get() )); } diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java index d0f6fcb..8221edc 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/io/MavenLoggerTest.java @@ -19,6 +19,7 @@ import org.apache.maven.plugin.logging.Log; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -61,418 +62,497 @@ void after() { Mockito.verifyNoMoreInteractions(log); } - @Test - void isTraceEnabled() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - boolean result = logger.isTraceEnabled(); - - // then - assertThat(result).isTrue(); - verify(log).isDebugEnabled(); + @Nested + class LevelEnabled extends Cases { + @Override + boolean isEnabled() { + return true; + } } - @Test - void trace() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.trace(MESSAGE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); + @Nested + class LevelDisabled extends Cases { + @Override + boolean isEnabled() { + return false; + } } - @Test - void testTrace() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_ONE, ARG_ONE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); + abstract class Cases { + abstract boolean isEnabled(); + + @Test + void isTraceEnabled() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + boolean result = logger.isTraceEnabled(); + + // then + assertThat(result).isEqualTo(isEnabled()); + verify(log).isDebugEnabled(); + } + + @Test + void trace() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testTrace() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testTrace1() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testTrace2() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.trace( + FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE + ); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testTrace3() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.trace(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString(), any()); + } + } + + @Test + void isDebugEnabled() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + boolean result = logger.isDebugEnabled(); + + // then + assertThat(result).isEqualTo(isEnabled()); + verify(log).isDebugEnabled(); + } + + @Test + void debug() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testDebug() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testDebug1() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testDebug2() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.debug( + FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE + ); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString()); + } + } + + @Test + void testDebug3() { + // given + when(log.isDebugEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.debug(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isDebugEnabled(); + if (isEnabled()) { + verify(log).debug(anyString(), any()); + } + } + + @Test + void isInfoEnabled() { + // given + when(log.isInfoEnabled()).thenReturn(isEnabled()); + + // when + boolean result = logger.isInfoEnabled(); + + // then + assertThat(result).isEqualTo(isEnabled()); + verify(log).isInfoEnabled(); + } + + @Test + void info() { + // given + when(log.isInfoEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.info(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + if (isEnabled()) { + verify(log).info(anyString()); + } + } + + @Test + void testInfo() { + // given + when(log.isInfoEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.info(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + if (isEnabled()) { + verify(log).info(anyString()); + } + } + + @Test + void testInfo1() { + // given + when(log.isInfoEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.info(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + if (isEnabled()) { + verify(log).info(anyString()); + } + } + + @Test + void testInfo2() { + // given + when(log.isInfoEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.info( + FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE + ); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + if (isEnabled()) { + verify(log).info(anyString()); + } + } + + @Test + void testInfo3() { + // given + when(log.isInfoEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.info(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isInfoEnabled(); + if (isEnabled()) { + verify(log).info(anyString(), any()); + } + } + + @Test + void isWarnEnabled() { + // given + when(log.isWarnEnabled()).thenReturn(isEnabled()); + + // when + boolean result = logger.isWarnEnabled(); + + // then + assertThat(result).isEqualTo(isEnabled()); + verify(log).isWarnEnabled(); + } + + @Test + void warn() { + // given + when(log.isWarnEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + if (isEnabled()) { + verify(log).warn(anyString()); + } + } + + @Test + void testWarn() { + // given + when(log.isWarnEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + if (isEnabled()) { + verify(log).warn(anyString()); + } + } + + @Test + void testWarn1() { + // given + when(log.isWarnEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + if (isEnabled()) { + verify(log).warn(anyString()); + } + } + + @Test + void testWarn2() { + // given + when(log.isWarnEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.warn( + FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE + ); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + if (isEnabled()) { + verify(log).warn(anyString()); + } + } + + @Test + void testWarn3() { + // given + when(log.isWarnEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.warn(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isWarnEnabled(); + if (isEnabled()) { + verify(log).warn(anyString(), any()); + } + } + + @Test + void isErrorEnabled() { + // given + when(log.isErrorEnabled()).thenReturn(isEnabled()); + + // when + boolean result = logger.isErrorEnabled(); + + // then + assertThat(result).isEqualTo(isEnabled()); + verify(log).isErrorEnabled(); + } + + @Test + void error() { + // given + when(log.isErrorEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.error(MESSAGE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + if (isEnabled()) { + verify(log).error(anyString()); + } + } + + @Test + void testError() { + // given + when(log.isErrorEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.error(FORMAT_ONE, ARG_ONE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + if (isEnabled()) { + verify(log).error(anyString()); + } + } + + @Test + void testError1() { + // given + when(log.isErrorEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.error(FORMAT_TWO, ARG_ONE, ARG_TWO); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + if (isEnabled()) { + verify(log).error(anyString()); + } + } + + @Test + void testError2() { + // given + when(log.isErrorEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.error(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + if (isEnabled()) { + verify(log).error(anyString()); + } + } + + @Test + void testError3() { + // given + when(log.isErrorEnabled()).thenReturn(isEnabled()); + + // when + ThrowingCallable throwingCallable = () -> logger.error(MESSAGE, THROW); + + // then + assertThatCode(throwingCallable).doesNotThrowAnyException(); + verify(log).isErrorEnabled(); + if (isEnabled()) { + verify(log).error(anyString(), any()); + } + } } - @Test - void testTrace1() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_TWO, ARG_ONE, ARG_TWO); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); - } - - @Test - void testTrace2() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.trace(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); - } - - @Test - void testTrace3() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.trace(MESSAGE, THROW); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString(), any()); - } - - @Test - void isDebugEnabled() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - boolean result = logger.isDebugEnabled(); - - // then - assertThat(result).isTrue(); - verify(log).isDebugEnabled(); - } - - @Test - void debug() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.debug(MESSAGE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); - } - - @Test - void testDebug() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_ONE, ARG_ONE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); - } - - @Test - void testDebug1() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_TWO, ARG_ONE, ARG_TWO); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); - } - - @Test - void testDebug2() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.debug(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString()); - } - - @Test - void testDebug3() { - // given - when(log.isDebugEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.debug(MESSAGE, THROW); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isDebugEnabled(); - verify(log).debug(anyString(), any()); - } - - @Test - void isInfoEnabled() { - // given - when(log.isInfoEnabled()).thenReturn(true); - - // when - boolean result = logger.isInfoEnabled(); - - // then - assertThat(result).isTrue(); - verify(log).isInfoEnabled(); - } - - @Test - void info() { - // given - when(log.isInfoEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.info(MESSAGE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isInfoEnabled(); - verify(log).info(anyString()); - } - - @Test - void testInfo() { - // given - when(log.isInfoEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.info(FORMAT_ONE, ARG_ONE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isInfoEnabled(); - verify(log).info(anyString()); - } - - @Test - void testInfo1() { - // given - when(log.isInfoEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.info(FORMAT_TWO, ARG_ONE, ARG_TWO); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isInfoEnabled(); - verify(log).info(anyString()); - } - - @Test - void testInfo2() { - // given - when(log.isInfoEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.info(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isInfoEnabled(); - verify(log).info(anyString()); - } - - @Test - void testInfo3() { - // given - when(log.isInfoEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.info(MESSAGE, THROW); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isInfoEnabled(); - verify(log).info(anyString(), any()); - } - - @Test - void isWarnEnabled() { - // given - when(log.isWarnEnabled()).thenReturn(true); - - // when - boolean result = logger.isWarnEnabled(); - - // then - assertThat(result).isTrue(); - verify(log).isWarnEnabled(); - } - - @Test - void warn() { - // given - when(log.isWarnEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.warn(MESSAGE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isWarnEnabled(); - verify(log).warn(anyString()); - } - - @Test - void testWarn() { - // given - when(log.isWarnEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_ONE, ARG_ONE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isWarnEnabled(); - verify(log).warn(anyString()); - } - - @Test - void testWarn1() { - // given - when(log.isWarnEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_TWO, ARG_ONE, ARG_TWO); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isWarnEnabled(); - verify(log).warn(anyString()); - } - - @Test - void testWarn2() { - // given - when(log.isWarnEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.warn(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isWarnEnabled(); - verify(log).warn(anyString()); - } - - @Test - void testWarn3() { - // given - when(log.isWarnEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.warn(MESSAGE, THROW); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isWarnEnabled(); - verify(log).warn(anyString(), any()); - } - - @Test - void isErrorEnabled() { - // given - when(log.isErrorEnabled()).thenReturn(true); - - // when - boolean result = logger.isErrorEnabled(); - - // then - assertThat(result).isTrue(); - verify(log).isErrorEnabled(); - } - - @Test - void error() { - // given - when(log.isErrorEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.error(MESSAGE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isErrorEnabled(); - verify(log).error(anyString()); - } - - @Test - void testError() { - // given - when(log.isErrorEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.error(FORMAT_ONE, ARG_ONE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isErrorEnabled(); - verify(log).error(anyString()); - } - - @Test - void testError1() { - // given - when(log.isErrorEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.error(FORMAT_TWO, ARG_ONE, ARG_TWO); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isErrorEnabled(); - verify(log).error(anyString()); - } - - @Test - void testError2() { - // given - when(log.isErrorEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.error(FORMAT_THREE, ARG_ONE, ARG_TWO, ARG_THREE); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isErrorEnabled(); - verify(log).error(anyString()); - } - - @Test - void testError3() { - // given - when(log.isErrorEnabled()).thenReturn(true); - - // when - ThrowingCallable throwingCallable = () -> logger.error(MESSAGE, THROW); - - // then - assertThatCode(throwingCallable).doesNotThrowAnyException(); - verify(log).isErrorEnabled(); - verify(log).error(anyString(), any()); - } } diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java index 0cf139c..1e422b8 100644 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java +++ b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/Library.java @@ -17,7 +17,7 @@ package pl.wavesoftware.plugs.tools.packager.api.model; import javax.annotation.Nullable; -import java.io.File; +import java.nio.file.Path; /** * Encapsulates information about a single library that may be packed into @@ -30,32 +30,32 @@ */ public final class Library { private final String name; - private final File file; + private final Path path; private final LibraryScope scope; /** * Create a new {@link Library}. - * @param file the source file + * @param path the source file * @param scope the scope of the library */ - public Library(File file, LibraryScope scope) { - this(null, file, scope); + public Library(Path path, LibraryScope scope) { + this(null, path, scope); } /** * Create a new {@link Library}. * @param name the name of the library as it should be written or {@code null} to use * the file name - * @param file the source file + * @param path the source file * @param scope the scope of the library */ public Library( @Nullable String name, - File file, + Path path, LibraryScope scope ) { - this.name = (name != null) ? name : file.getName(); - this.file = file; + this.name = (name != null) ? name : path.getFileName().toString(); + this.path = path; this.scope = scope; } @@ -71,8 +71,8 @@ public String getName() { * Return the library file. * @return the file */ - public File getFile() { - return this.file; + public Path getPath() { + return this.path; } /** diff --git a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java index 82bbf40..04a0205 100644 --- a/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java +++ b/tools/plugs-packager-api/src/test/java/pl/wavesoftware/plugs/tools/packager/api/jar/LibrariesCollectorTest.java @@ -20,9 +20,10 @@ import pl.wavesoftware.plugs.tools.packager.api.model.Library; import pl.wavesoftware.plugs.tools.packager.api.model.LibraryScope; -import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Krzysztof Suszynski @@ -34,9 +35,9 @@ class LibrariesCollectorTest { void getCollected() { // given LibrariesCollector collector = new LibrariesCollector(); - File acmeFile = new File("acme"); + Path acmePath = Paths.get("acme"); LibraryScope scope = LibraryScope.PROVIDED; - Library library = new Library(acmeFile, scope); + Library library = new Library(acmePath, scope); // when collector.collect(library); @@ -44,9 +45,9 @@ void getCollected() { // then assertThat(collector.getCollected()).hasSize(1); assertThat(collector.getCollected().head()).extracting( - Library::getFile, Library::getScope, Library::getName + Library::getPath, Library::getScope, Library::getName ).containsOnly( - acmeFile, scope, "acme" + acmePath, scope, "acme" ); assertThat(scope.toString()).isEqualTo("provided"); } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java index d1b5e5b..a5aeda9 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/CrcAndSize.java @@ -19,10 +19,10 @@ import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import javax.annotation.WillNotClose; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.zip.CRC32; import java.util.zip.ZipEntry; @@ -39,8 +39,8 @@ final class CrcAndSize { private long size; - CrcAndSize(File file) throws IOException { - try (FileInputStream inputStream = new FileInputStream(file)) { + CrcAndSize(Path path) throws IOException { + try (InputStream inputStream = Files.newInputStream(path)) { load(inputStream); } } diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java index 0c9a0c1..6e56039 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriter.java @@ -23,41 +23,51 @@ import pl.wavesoftware.plugs.tools.packager.api.model.Library; import javax.annotation.Nullable; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Enumeration; import java.util.function.Supplier; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute; + /** * @author Krzysztof Suszynski * @since 0.1.0 */ final class LibrariesJarWriter { - private static final Logger LOGGER = + private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(LibrariesJarWriter.class); + private final Logger logger; private final Supplier listenersSupplier; private final JarEntryWriter entryWriter; LibrariesJarWriter( + Supplier listenersSupplier, JarEntryWriter entryWriter + ) { + this(DEFAULT_LOGGER, listenersSupplier, entryWriter); + } + + LibrariesJarWriter( + Logger logger, Supplier listenersSupplier, JarEntryWriter entryWriter ) { + this.logger = logger; this.listenersSupplier = listenersSupplier; this.entryWriter = entryWriter; } void writeLibrary(String destination, Library library) throws IOException { - File file = library.getFile(); + Path path = library.getPath(); JarArchiveEntry entry = new JarArchiveEntry(destination + library.getName()); - entry.setTime(getNestedLibraryTime(file)); - new CrcAndSize(file).setupStoredEntry(entry); + entry.setTime(getNestedLibraryTime(path)); + new CrcAndSize(path).setupStoredEntry(entry); entryWriter.writeEntry( - entry, - new InputStreamEntryWriter(new FileInputStream(file), true) + entry, new InputStreamEntryWriter(Files.newInputStream(path), true) ); LibraryHasBeenWritten event = new LibraryHasBeenWritten(library); Listeners listeners = listenersSupplier.get(); @@ -66,9 +76,9 @@ void writeLibrary(String destination, Library library) throws IOException { ); } - private static long getNestedLibraryTime(File file) { + private long getNestedLibraryTime(Path path) throws IOException { try { - try (JarFile jarFile = new JarFile(file)) { + try (JarFile jarFile = new JarFile(path.toFile())) { Enumeration entries = jarFile.entries(); Long entry = findTimeOfJarEntries(entries); if (entry != null) { @@ -77,16 +87,16 @@ private static long getNestedLibraryTime(File file) { } } catch (IOException ex) { // Ignore and just use the source file timestamp - if (LOGGER.isTraceEnabled()) { - LOGGER.trace( + if (logger.isTraceEnabled()) { + logger.trace( MessageFormatter.format( - "Can't read a supposed JAR file: {}", file - ).toString(), + "Can't read a supposed JAR file: {}", path + ).getMessage(), ex ); } } - return file.lastModified(); + return tryToExecute(() -> Files.getLastModifiedTime(path).toMillis(), "20190924:230826"); } @Nullable diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java index a0a54b9..5af9ac8 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/jar/WritableLibraries.java @@ -37,7 +37,7 @@ public final class WritableLibraries { public WritableLibraries(Libraries libraries) throws IOException { libraries.doWithLibraries(library -> { - IsZipFile isZipFile = new IsZipFile(library.getFile().toPath()); + IsZipFile isZipFile = new IsZipFile(library.getPath()); if (isZipFile.getAsBoolean()) { Library existing = this.libraryEntryNames.putIfAbsent( LIBRARY_DESTINATION + library.getName(), diff --git a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java index a52adb9..2f8b043 100644 --- a/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java +++ b/tools/plugs-packager-core/src/main/java/pl/wavesoftware/plugs/tools/packager/core/manifest/ManifestBuilderImpl.java @@ -66,7 +66,7 @@ public Manifest buildManifest(Project project, JarFile sourceJar) { } manifest = new Manifest(manifest); Attributes attributes = manifest.getMainAttributes(); - String plugsVersion = PlugsVersion.getVersion(); + String plugsVersion = PlugsVersion.get().getVersion(); CharSequence hash = tring(() -> digest.digest(project)).or( "Can't calculate digest from source jar: {}", sourcePath diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java new file mode 100644 index 0000000..df55998 --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; +import pl.wavesoftware.plugs.tools.packager.api.model.Library; +import pl.wavesoftware.plugs.tools.packager.api.model.LibraryScope; + +import java.io.IOException; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.matches; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Krzysztof Suszynski + * @since 0.1.0 + */ +@ExtendWith(MockitoExtension.class) +class LibrariesJarWriterTest { + + private final Listeners listeners = new Listeners(); + @Mock + private Logger logger; + @Mock + private JarEntryWriter entryWriter; + + @AfterEach + void after() { + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(logger, entryWriter); + } + + @Test + void writeLibrary() throws IOException { + // given + when(logger.isTraceEnabled()).thenReturn(true); + LibrariesJarWriter jarWriter = new LibrariesJarWriter( + logger, () -> listeners, entryWriter + ); + Library library = new Library(Paths.get("/path/to/guava.jar"), LibraryScope.RUNTIME); + String destination = "PLUGS-INF"; + + // when + ThrowingCallable throwingCallable = () -> jarWriter.writeLibrary(destination, library); + + // then + assertThatCode(throwingCallable) + .hasCauseInstanceOf(IOException.class) + .hasMessageContaining("/path/to/guava.jar") + .hasMessageContaining("20190924:230826"); + verify(logger).isTraceEnabled(); + verify(logger).trace( + matches("^Can't read a supposed JAR file.+"), any(IOException.class) + ); + } +} diff --git a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java index f5d2cfe..259ca80 100644 --- a/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java +++ b/tools/plugs-packager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/artifact/MavenlikeArtifact.java @@ -86,13 +86,4 @@ private String finalName() { name(), version(), type().extension() ); } - - @Override - public String toString() { - return "MavenlikeArtifact{" + - "name='" + name + '\'' + - ", group='" + group + '\'' + - ", version=" + version + - '}'; - } } From da213ed0887cfb5bbe62175531b7cc0f795e7276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Wed, 25 Sep 2019 00:17:37 +0200 Subject: [PATCH 22/23] Tests for LibrariesJarWriter and PackagePlugMojoIT And couple other classes to fix SonarQube issues --- .../mapper/MavenPackagerCoordinates.java | 6 +- .../tools/maven/plugin/PackagePlugMojoIT.java | 5 + .../plugin/mapper/ArtifactMapperImplTest.java | 60 ++++++++++ .../mapper/MavenPackagerCoordinatesTest.java | 106 ++++++++++++++++++ .../packager/api/model/StandardLibraries.java | 35 ------ .../core/jar/JarFileEntriesWriterTest.java | 66 +++++++++++ .../core/jar/LibrariesJarWriterTest.java | 24 +++- .../src/test/resources/composite.zip | Bin 0 -> 469 bytes 8 files changed, 261 insertions(+), 41 deletions(-) create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImplTest.java create mode 100644 tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinatesTest.java delete mode 100644 tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java create mode 100644 tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriterTest.java create mode 100644 tools/plugs-packager-core/src/test/resources/composite.zip diff --git a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java index c4b438e..a8c7074 100644 --- a/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java +++ b/tools/plugs-maven-plugin/src/main/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinates.java @@ -59,10 +59,8 @@ private Path calculateTargetPath() { if (!classifier.isEmpty() && !classifier.startsWith("-")) { classifier = "-" + classifier; } - //noinspection RedundantIfStatement - if (!project.outputPath().toFile().exists()) { - assert project.outputPath().toFile().mkdirs(); - } + //noinspection ResultOfMethodCallIgnored + project.outputPath().toFile().mkdirs(); return project.outputPath().resolve(plugFileNameFor(project, classifier)); } diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java index d93ba7f..c2546ec 100644 --- a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/PackagePlugMojoIT.java @@ -156,6 +156,11 @@ class CodeWithDependencies extends BaseMavenTestCase { @Test @DisplayName("Execute mojo on code-with-deps project") void execute(MojoFactory factory) { + assertThatCode(() -> perform(factory)).doesNotThrowAnyException(); + assertThatCode(() -> perform(factory)).doesNotThrowAnyException(); + } + + private void perform(MojoFactory factory) { // given PackagePlugMojo mojo = factory .customizer(getCustomizer()) diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImplTest.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImplTest.java new file mode 100644 index 0000000..27c53cc --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/ArtifactMapperImplTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.repository.RepositorySystem; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; + +import static org.assertj.core.api.Assertions.assertThatCode; + +@ExtendWith(MockitoExtension.class) +class ArtifactMapperImplTest { + @Mock + private RepositorySystem repositorySystem; + @Mock + private ArtifactResolver artifactResolver; + @Mock + private Artifact artifact; + + @AfterEach + void after() { + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(repositorySystem, artifactResolver); + } + + @Test + void mavenize() { + // given + ArtifactMapper mapper = new ArtifactMapperImpl(repositorySystem, artifactResolver); + + // when + ThrowingCallable throwingCallable = () -> mapper.mavenize(artifact); + + // then + assertThatCode(throwingCallable) + .isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("Not supported artifact type"); + } +} diff --git a/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinatesTest.java b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinatesTest.java new file mode 100644 index 0000000..b93079f --- /dev/null +++ b/tools/plugs-maven-plugin/src/test/java/pl/wavesoftware/plugs/tools/maven/plugin/mapper/MavenPackagerCoordinatesTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.maven.plugin.mapper; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import pl.wavesoftware.plugs.tools.packager.api.model.Artifact; +import pl.wavesoftware.plugs.tools.packager.api.model.ArtifactType; +import pl.wavesoftware.plugs.tools.packager.api.model.Project; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class MavenPackagerCoordinatesTest { + + @Mock + private Project project; + @Mock + private Artifact artifact; + + @AfterEach + void after() { + Mockito.validateMockitoUsage(); + Mockito.verifyNoMoreInteractions(project, artifact); + } + + @Test + void targetPath() throws IOException { + // given + stub("plx"); + MavenPackagerCoordinates coordinates = + new MavenPackagerCoordinates(() -> project); + + // when + Path result1 = coordinates.targetPath(); + Path result2 = coordinates.targetPath(); + + // then + assertThat(result1.toString()).endsWith("acmeapp-plx.jar"); + assertThat(result1).isEqualTo(result2); + } + + @Test + void withHypenOnClassifier() throws IOException { + // given + stub("-plx"); + MavenPackagerCoordinates coordinates = + new MavenPackagerCoordinates(() -> project); + + // when + Path result = coordinates.targetPath(); + + // then + assertThat(result.toString()).endsWith("acmeapp-plx.jar"); + } + + @Test + void withEmptyClassifier() throws IOException { + // given + stub(""); + MavenPackagerCoordinates coordinates = + new MavenPackagerCoordinates(() -> project); + + // when + Path result = coordinates.targetPath(); + + // then + assertThat(result.toString()).endsWith("acmeapp.jar"); + } + + private void stub(String classifier) throws IOException { + when(project.classifier()).thenReturn(classifier); + Path dir = Paths.get( + System.getProperty("java.io.tmpdir"), "depth", "path", "inside" + ); + Files.deleteIfExists(dir); + when(project.outputPath()).thenReturn(dir); + when(project.finalName()).thenReturn("acmeapp"); + when(project.mainArtifact()).thenReturn(artifact); + when(artifact.type()).thenReturn(ArtifactType.JAR); + } +} diff --git a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java b/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java deleted file mode 100644 index b979913..0000000 --- a/tools/plugs-packager-api/src/main/java/pl/wavesoftware/plugs/tools/packager/api/model/StandardLibraries.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2019 Wave Software - * - * 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 pl.wavesoftware.plugs.tools.packager.api.model; - -/** - * A class that holds list of standard libraries - * - * @author Krzysztof Suszynski - * @since 0.1.0 - */ -public final class StandardLibraries { - - /** - * Represents no libraries. - */ - public static final Libraries NONE = callback -> {}; - - private StandardLibraries() { - // non reachable - } -} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriterTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriterTest.java new file mode 100644 index 0000000..000a64d --- /dev/null +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/JarFileEntriesWriterTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019 Wave Software + * + * 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 pl.wavesoftware.plugs.tools.packager.core.jar; + +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; +import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; +import org.junit.jupiter.api.Test; +import pl.wavesoftware.plugs.tools.packager.core.jar.JarArchiveEntryWriter.Context; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collection; +import java.util.jar.JarFile; + +import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull; + +class JarFileEntriesWriterTest { + + @Test + void writeEntries() throws IOException, URISyntaxException { + // given + try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { + Path path = fs.getPath("JarFileEntriesWriterTest", "target.jar"); + Files.createDirectories(path.getParent()); + Collection registeredEntries = new ArrayList<>(); + try (OutputStream out = Files.newOutputStream(path); + JarArchiveOutputStream jarOutputStream = new JarArchiveOutputStream(out)) { + Context context = new Context(() -> registeredEntries, () -> jarOutputStream); + JarArchiveEntryWriter jarArchiveEntryWriter = + new JarArchiveEntryWriter(context); + JarFileEntriesWriter jarFileEntriesWriter = + new JarFileEntriesWriter(jarArchiveEntryWriter); + URL url = checkNotNull( + getClass().getClassLoader().getResource("composite.zip"), + "20190925:124819" + ); + JarFile jarFile = new JarFile(Paths.get(url.toURI()).toFile()); + + // when + jarFileEntriesWriter.writeEntries(jarFile, new IdentityEntryTransformer()); + } + } + } +} diff --git a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java index df55998..74793ff 100644 --- a/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java +++ b/tools/plugs-packager-core/src/test/java/pl/wavesoftware/plugs/tools/packager/core/jar/LibrariesJarWriterTest.java @@ -56,7 +56,7 @@ void after() { } @Test - void writeLibrary() throws IOException { + void writeLibrary() { // given when(logger.isTraceEnabled()).thenReturn(true); LibrariesJarWriter jarWriter = new LibrariesJarWriter( @@ -71,11 +71,31 @@ void writeLibrary() throws IOException { // then assertThatCode(throwingCallable) .hasCauseInstanceOf(IOException.class) - .hasMessageContaining("/path/to/guava.jar") + .hasMessageContaining("guava.jar") .hasMessageContaining("20190924:230826"); verify(logger).isTraceEnabled(); verify(logger).trace( matches("^Can't read a supposed JAR file.+"), any(IOException.class) ); } + + @Test + void withoutTrace() { + // given + LibrariesJarWriter jarWriter = new LibrariesJarWriter( + logger, () -> listeners, entryWriter + ); + Library library = new Library(Paths.get("/path/to/guava.jar"), LibraryScope.RUNTIME); + String destination = "PLUGS-INF"; + + // when + ThrowingCallable throwingCallable = () -> jarWriter.writeLibrary(destination, library); + + // then + assertThatCode(throwingCallable) + .hasCauseInstanceOf(IOException.class) + .hasMessageContaining("guava.jar") + .hasMessageContaining("20190924:230826"); + verify(logger).isTraceEnabled(); + } } diff --git a/tools/plugs-packager-core/src/test/resources/composite.zip b/tools/plugs-packager-core/src/test/resources/composite.zip new file mode 100644 index 0000000000000000000000000000000000000000..5f42af5319eec82e002a287f2f6b4b0255fa9e6d GIT binary patch literal 469 zcmWIWW@Zs#U|`^2Fm1B%fAKZ{;A9|gGZ1qFacV_kZb43}UR7p6X^$<}AqSC`^vC=e zT-uHs1UKHh+TYY%huy7HcT>sKwaSNgl-XY}m@X$wqxd=DG;a<@Kk ziCnNUxh+YJb=9#tO~Iao|C83Q-|6yk-h3<8_a(*scQ!xK@?R1FbUPQ&l?)6ysh0lJ zm(Ba70%U_QClCj@I=c9}>gA?5=jRsW7iX5FDgeE~rHcSuT+aDEO#asd2j7;_n zxB>!XBnT*gNt9sWf(kM+NH9EFu Date: Wed, 25 Sep 2019 13:02:29 +0200 Subject: [PATCH 23/23] Remove CoodeCov in favor of SonarCloud --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4416fd8..c84d94c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,8 +11,6 @@ matrix: # Quality testing - jdk: oraclejdk8 env: JACOCO=true RELEASE_CHECKS=true - after_success: - - bash <(curl -s https://codecov.io/bash) - jdk: oraclejdk8 env: JACOCO=true SONAR=publish # Performance testing