Skip to content

Commit

Permalink
Working IT cases with proper manifest basic entries
Browse files Browse the repository at this point in the history
Krzysztof Suszynski authored and cardil committed Jun 6, 2019
1 parent 46084d9 commit a181508
Showing 15 changed files with 246 additions and 4 deletions.
21 changes: 21 additions & 0 deletions plug-api/pom.xml
Original file line number Diff line number Diff line change
@@ -32,5 +32,26 @@

<properties>
<project.rootdir>${project.basedir}/..</project.rootdir>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencies>
<dependency>
<groupId>pl.wavesoftware</groupId>
<artifactId>eid-exceptions</artifactId>
</dependency>

<!-- Tests -->
<dependency>
<groupId>pl.wavesoftware.plugs</groupId>
<artifactId>testing</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.zafarkhaja</groupId>
<artifactId>java-semver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
73 changes: 73 additions & 0 deletions plug-api/src/main/java/pl/wavesoftware/plugs/api/PlugsVersion.java
Original file line number Diff line number Diff line change
@@ -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<String> maybeVersion =
Optional.ofNullable(
PlugsVersion.class.getPackage().getImplementationVersion()
);
return checkNotNull(
maybeVersion.orElseGet(PlugsVersion::manuallyRead),
"20190325:202509"
);
}

static String manuallyRead() {
List<URL> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions plugs-core/pom.xml
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@

<properties>
<project.rootdir>${project.basedir}/..</project.rootdir>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencies>
1 change: 1 addition & 0 deletions plugs-felix/pom.xml
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@

<properties>
<project.rootdir>${project.basedir}/..</project.rootdir>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencies>
1 change: 1 addition & 0 deletions plugs-spring/pom.xml
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@

<properties>
<project.rootdir>${project.basedir}/..</project.rootdir>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencies>
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -106,6 +106,7 @@
<sonar.java.source>${java.source.version}</sonar.java.source>
<maven.compiler.source>1.${java.source.version}</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<unpack-manifestmf.skip>true</unpack-manifestmf.skip>

<skipTests/>
<coveralls.skip>${skipTests}</coveralls.skip>
@@ -273,6 +274,22 @@
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Artifact-Id>${project.artifactId}</Implementation-Artifact-Id>
</manifestEntries>
</archive>
</configuration>
</plugin>

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>

<plugin>
@@ -373,6 +390,32 @@
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-manifest-mf</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<skip>${unpack-manifestmf.skip}</skip>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
<includes>META-INF/MANIFEST.MF</includes>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

1 change: 1 addition & 0 deletions testing/pom.xml
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@

<properties>
<project.rootdir>${project.basedir}/..</project.rootdir>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencies>
4 changes: 4 additions & 0 deletions tools/plugs-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -35,6 +35,10 @@
<maven>3.3</maven>
</prerequisites>

<properties>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
10 changes: 10 additions & 0 deletions tools/plugs-packager-core/pom.xml
Original file line number Diff line number Diff line change
@@ -29,8 +29,18 @@

<artifactId>plugs-packager-core</artifactId>
<name>Plugs :: Tools :: Packager Core</name>

<properties>
<unpack-manifestmf.skip>false</unpack-manifestmf.skip>
</properties>

<dependencies>
<dependency>
<groupId>pl.wavesoftware.plugs</groupId>
<artifactId>plug-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>pl.wavesoftware</groupId>
<artifactId>eid-exceptions</artifactId>
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
}

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a181508

Please sign in to comment.