Skip to content

Scopes filtering to jar added in the spring boot jar #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ activemq-data
overridedb.*
*.iml
.idea
.java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-scopes-filtering</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<scopes>
<scope>compile</scope>
<scope>runtime</scope>
</scopes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>some.random.Main</mainClass>
</manifest>
<manifestEntries>
<Not-Used>Foo</Not-Used>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>@slf4j.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.test;

public class SampleApplication {

public static void main(String[] args) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.io.*;
import org.springframework.boot.maven.*;

Verify.verifyJar(
new File( basedir, "target/jar-scopes-filtering-0.0.1.BUILD-SNAPSHOT.jar" ), "some.random.Main"
);

Verify.verifyJarContain(
new File( basedir, "target/jar-scopes-filtering-0.0.1.BUILD-SNAPSHOT.jar" ), "some.random.Main", "lib/spring-context-3.2.3.RELEASE.jar"
);

Verify.verifyJarNotContain(
new File( basedir, "target/jar-scopes-filtering-0.0.1.BUILD-SNAPSHOT.jar" ), "some.random.Main", "lib/jcl-over-slf4j-1.7.6.jar"
);

Verify.verifyJarContain(
new File( basedir, "target/jar-scopes-filtering-0.0.1.BUILD-SNAPSHOT.jar" ), "some.random.Main", "lib/spring-beans-3.2.3.RELEASE.jar"
);


Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.springframework.boot.maven;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -35,26 +37,34 @@
*/
public class ArtifactsLibraries implements Libraries {

private static final Map<String, LibraryScope> SCOPES;
private static final Map<String, LibraryScope> DEFAULT_SCOPES;
static {
Map<String, LibraryScope> scopes = new HashMap<String, LibraryScope>();
scopes.put(Artifact.SCOPE_COMPILE, LibraryScope.COMPILE);
scopes.put(Artifact.SCOPE_RUNTIME, LibraryScope.RUNTIME);
scopes.put(Artifact.SCOPE_PROVIDED, LibraryScope.PROVIDED);
SCOPES = Collections.unmodifiableMap(scopes);
DEFAULT_SCOPES = Collections.unmodifiableMap(scopes);
}

private final Set<Artifact> artifacts;

private final List<String> userScopes;

public ArtifactsLibraries(Set<Artifact> artifacts) {
this.artifacts = artifacts;
this.userScopes = new ArrayList<String>( DEFAULT_SCOPES.keySet() );
}

public ArtifactsLibraries(Set<Artifact> artifacts, List<String> userScopes) {
this.artifacts = artifacts;
this.userScopes = ( userScopes == null || userScopes.isEmpty() ) ? new ArrayList<String>( DEFAULT_SCOPES.keySet() ) : userScopes;
}

@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
for (Artifact artifact : this.artifacts) {
LibraryScope scope = SCOPES.get(artifact.getScope());
if (scope != null && artifact.getFile() != null) {
LibraryScope scope = DEFAULT_SCOPES.get(artifact.getScope());
if (scope != null && artifact.getFile() != null && userScopes.contains( artifact.getScope() )) {
callback.library(artifact.getFile(), scope);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarFile;

Expand Down Expand Up @@ -95,6 +96,12 @@ public class RepackageMojo extends AbstractMojo {
@Parameter
private LayoutType layout;

/**
* list of artifact scopes you want to have repackaged in the jar
*/
@Parameter
private List<String> scopes;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
File source = this.project.getArtifact().getFile();
Expand Down Expand Up @@ -122,7 +129,7 @@ protected String findMainMethod(JarFile source) throws IOException {
getLog().info("Layout: " + this.layout);
repackager.setLayout(this.layout.layout());
}
Libraries libraries = new ArtifactsLibraries(this.project.getArtifacts());
Libraries libraries = new ArtifactsLibraries(this.project.getArtifacts(), this.scopes);
try {
repackager.repackage(target, libraries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

/**
* Verification utility for use with maven-invoker-plugin verification scripts.
Expand All @@ -38,6 +39,14 @@ public static void verifyJar(File file) throws Exception {
new JarArchiveVerification(file, "org.test.SampleApplication").verify();
}

public static void verifyJarContain(File file, String main, String path) throws Exception {
new JarArchiveVerification(file, main).verify(path);
}

public static void verifyJarNotContain(File file, String main, String path) throws Exception {
new JarArchiveVerification(file, main).verifyNot(path);
}

public static void verifyJar(File file, String main) throws Exception {
new JarArchiveVerification(file, main).verify();
}
Expand Down Expand Up @@ -73,6 +82,39 @@ public void verify() throws Exception {
zipFile.close();
}

public void verify(String path) throws Exception {
assertTrue("Archive missing", this.file.exists());
assertTrue("Archive not a file", this.file.isFile());

ZipFile zipFile = new ZipFile(this.file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
Map<String, ZipEntry> zipMap = new HashMap<String, ZipEntry>();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
zipMap.put(zipEntry.getName(), zipEntry);
}
verifyZipEntries(zipFile, zipMap);
assertTrue("archive not containing file with path:" + path, zipMap.containsKey( path ) );
zipFile.close();
}

public void verifyNot(String path) throws Exception {
assertTrue("Archive missing", this.file.exists());
assertTrue("Archive not a file", this.file.isFile());

ZipFile zipFile = new ZipFile(this.file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
Map<String, ZipEntry> zipMap = new HashMap<String, ZipEntry>();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
zipMap.put(zipEntry.getName(), zipEntry);
}
verifyZipEntries(zipFile, zipMap);
assertFalse("archive containing file with path:" + path, zipMap.containsKey( path ) );
zipFile.close();
}


protected void verifyZipEntries(ZipFile zipFile, Map<String, ZipEntry> entries)
throws Exception {
verifyManifest(zipFile, entries.get("META-INF/MANIFEST.MF"));
Expand All @@ -98,7 +140,7 @@ protected final void assertHasEntryNameStartingWith(

private static class JarArchiveVerification extends AbstractArchiveVerification {

private final String main;
private String main;

public JarArchiveVerification(File file, String main) {
super(file);
Expand Down