Skip to content

Commit

Permalink
Removing unpacks, adding code to properly handle Maven dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Suszynski authored and cardil committed Jun 6, 2019
1 parent a181508 commit d675ab5
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ final class MavenFilter implements Filter {
public Set<Artifact> filterDependencies(Set<Artifact> dependencies)
throws RepackageFailed {
try {
Set<Artifact> 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,19 +89,19 @@ public Set<Artifact> imports() {

private Set<Artifact> calculateDependencies() {
return HashSet
.ofAll(mavenProject.getArtifacts())
.ofAll(mavenProject.getDependencies())
.reject(MavenBackedProject::hasProvidedScope)
.map(artifactMapper::generalize);
.map(artifactMapper::map);
}

private Set<Artifact> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<org.apache.maven.artifact.Artifact> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ final class MavenLibrariesFactory implements LibrariesFactory {
public Libraries create(Set<Artifact> artifacts, Logger logger) {
return new MavenLibraries(
artifacts.map(artifactMapper::mavenize),
null,
logger
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -51,17 +48,13 @@ public final class MavenLibraries implements Libraries {
.put(Artifact.SCOPE_SYSTEM, LibraryScope.PROVIDED);

private final Set<Artifact> artifacts;
@Nullable
private final Traversable<Dependency> unpacks;
private final Logger logger;

public MavenLibraries(
Set<Artifact> artifacts,
@Nullable Traversable<Dependency> unpacks,
Logger logger
) {
this.artifacts = artifacts;
this.unpacks = unpacks;
this.logger = logger;
}

Expand All @@ -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<String> getDuplicates(Set<Artifact> artifacts) {
java.util.Set<String> duplicates = new java.util.HashSet<>();
java.util.Set<String> seen = new java.util.HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

<E extends ArchiveWriterEvent> void addListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
}
}
Loading

0 comments on commit d675ab5

Please sign in to comment.