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] 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