Skip to content
Open
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package com.savage7.maven.plugin.dependency;

import com.google.api.translate.Language;
import com.google.api.translate.TranslateV2;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
* Hello world!
*
* Demonstrates that an externally-downloaded artifact (commons-io) is available
* on the classpath after the maven-external-dependency-plugin has resolved and
* installed it during the {@code process-resources} phase.
*/
public class App
{
public static void main( String[] args ) throws Exception
{
// Set the HTTP referrer to your website address.
TranslateV2 translate = new TranslateV2();
TranslateV2.setHttpReferrer("http://localhost");
public class App {
public static void main(String[] args) throws Exception {
// Write a temporary file and read it back using commons-io,
// which was downloaded by the external-dependency plugin.
File temp = File.createTempFile("external-dep-demo", ".txt");
temp.deleteOnExit();

String englishText = "Hello World";
String spanishTranslatedText = translate.execute(englishText, Language.ENGLISH, Language.SPANISH);
String frenchTranslatedText = translate.execute(englishText, Language.ENGLISH, Language.FRENCH);
String germanTranslatedText = translate.execute(englishText, Language.ENGLISH, Language.GERMAN);
FileUtils.writeStringToFile(temp, "Hello from commons-io!", StandardCharsets.UTF_8);
String content = FileUtils.readFileToString(temp, StandardCharsets.UTF_8);

System.out.println("ENLGISH : " + englishText);
System.out.println("SPANISH : " + spanishTranslatedText);
System.out.println("FRENCH : " + frenchTranslatedText);
System.out.println("GERMAN : " + germanTranslatedText);
System.out.println("commons-io is on the classpath: " + content);
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,78 @@
package com.savage7.maven.plugin.dependency;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* Unit test for simple App.
* Verifies that the maven-external-dependency-plugin correctly downloaded
* and staged the declared artifact items.
*
* The {@code resolve-external} and {@code install-external} goals run in the
* {@code process-resources} phase, which precedes the {@code test} phase, so
* the staging directory is populated by the time these tests execute.
*
* The staging directory path is injected via the {@code staging.directory}
* system property configured in the maven-surefire-plugin.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
public class AppTest {

/** Staging directory configured in pom.xml and passed as a system property. */
private static final String STAGING_DIR =
System.getProperty("staging.directory", "target/dependencies/");

@Test
public void testStagingDirectoryExists() {
File stagingDir = new File(STAGING_DIR);
assertTrue("Staging directory should exist after resolve-external goal: " + stagingDir.getAbsolutePath(),
stagingDir.exists() && stagingDir.isDirectory());
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
@Test
public void testStagingDirectoryIsNotEmpty() {
File stagingDir = new File(STAGING_DIR);
if (stagingDir.exists()) {
String[] files = stagingDir.list();
assertNotNull("Staging directory listing should not be null", files);
assertTrue("Staging directory should contain at least one downloaded artifact",
files.length > 0);
}
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
/** Example 1 – basic JAR downloaded via URL template. */
@Test
public void testCommonsIoDownloaded() {
assertStagedFileExists("commons-io-2.14.0.jar");
}

/** Example 2 – JAR downloaded with explicit SHA-1 checksum verification. */
@Test
public void testHamcrestCoreDownloaded() {
assertStagedFileExists("hamcrest-core-1.3.jar");
}

/** Example 3 – download-only artifact (install=false, deploy=false). */
@Test
public void testSlf4jApiDownloaded() {
assertStagedFileExists("slf4j-api-1.7.36.jar");
}

/** Example 4 – JAR with sources classifier. */
@Test
public void testCommonsLangSourcesDownloaded() {
assertStagedFileExists("commons-lang-2.6-sources.jar");
}

// -------------------------------------------------------------------------
// Helper
// -------------------------------------------------------------------------

private void assertStagedFileExists(String fileName) {
File artifact = new File(STAGING_DIR, fileName);
assertTrue("Expected staged artifact to exist and be non-empty: " + artifact.getAbsolutePath(),
artifact.exists() && artifact.length() > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
package com.savage7.maven.plugin.dependency;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -515,4 +520,56 @@ else if(artifactProperty.getNodeName().equalsIgnoreCase("version"))
);
}
}

/**
* Attempts to extract a POM file from within the JAR artifact.
* Maven-built JARs contain their POM at
* META-INF/maven/{groupId}/{artifactId}/pom.xml.
*
* @param artifactItem
* the artifact configuration
* @param jarFile
* the JAR file to search within
* @return extracted POM File, or <code>null</code> if not found
* @throws IOException
* if an I/O error occurs while reading the JAR
*/
protected File extractPomFromJar(ArtifactItem artifactItem, File jarFile)
throws IOException
{
String pomPath = "META-INF/maven/" + artifactItem.getGroupId()
+ "/" + artifactItem.getArtifactId() + "/pom.xml";

getLog().debug("looking for embedded POM in JAR at: " + pomPath);

try (JarFile jar = new JarFile(jarFile))
{
JarEntry entry = jar.getJarEntry(pomPath);
if (entry != null)
{
File tempPom = File.createTempFile(
artifactItem.getGroupId() + "." + artifactItem.getArtifactId(),
".pom");
tempPom.deleteOnExit();

try (InputStream is = jar.getInputStream(entry);
OutputStream os = new FileOutputStream(tempPom))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
os.write(buffer, 0, bytesRead);
}
}

getLog().info(
"extracted POM from JAR: " + pomPath);
return tempPom;
}
}

getLog().debug("no embedded POM found in JAR at: " + pomPath);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ public class ArtifactItem
* @parameter
*/
private boolean repack = false;

/**
* Flag whether to attempt extracting POM from the JAR's META-INF directory.
* When true (default), the plugin will look for an embedded POM at
* META-INF/maven/{groupId}/{artifactId}/pom.xml inside the JAR and use it
* instead of generating a minimal one.
*
* @parameter default-value="true"
*/
private Boolean extractPom = true;

/**
* default constructor.
Expand Down Expand Up @@ -668,4 +678,21 @@ public void setRepack(boolean repack)
this.repack = repack;
}

/**
* @return ExtractPom.
*/
public final Boolean getExtractPom()
{
return extractPom;
}

/**
* @param extractPom
* ExtractPom.
*/
public final void setExtractPom(final Boolean extractPom)
{
this.extractPom = extractPom;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,34 @@ public void execute() throws MojoExecutionException, MojoFailureException
}
else
{
// dynamically create a new POM file for this
// artifact
generatedPomFile = generatePomFile(artifactItem);
ArtifactMetadata pomMetadata = new ProjectArtifactMetadata(
artifact, generatedPomFile);
// try to extract POM from within the JAR
File extractedPom = null;
if (artifactItem.getExtractPom() == true
&& "jar".equals(artifactItem.getPackaging()))
{
extractedPom = extractPomFromJar(
artifactItem, installedArtifactFile);
}

if (artifactItem.getGeneratePom() == true)
if (extractedPom != null)
{
getLog().debug(
"deploying POM extracted from JAR: "
+ extractedPom.getAbsolutePath());
ArtifactMetadata pomMetadata =
new ProjectArtifactMetadata(
artifact, extractedPom);
artifact.addMetadata(pomMetadata);
generatedPomFile = extractedPom;
}
else if (artifactItem.getGeneratePom() == true)
{
// dynamically create a new POM file for this
// artifact
generatedPomFile = generatePomFile(artifactItem);
ArtifactMetadata pomMetadata =
new ProjectArtifactMetadata(
artifact, generatedPomFile);
artifact.addMetadata(pomMetadata);
}
}
Expand Down Expand Up @@ -193,6 +213,11 @@ public void execute() throws MojoExecutionException, MojoFailureException
throw new MojoExecutionException(
"Deployment of external dependency failed.", e);
}
catch (IOException e)
{
throw new MojoExecutionException(
"Error reading artifact file for deployment.", e);
}

}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,38 @@ public void execute() throws MojoExecutionException, MojoFailureException
}
else
{
// dynamically create a new POM file for
// this artifact
generatedPomFile = generatePomFile(artifactItem);
ArtifactMetadata pomMetadata = new ProjectArtifactMetadata(
artifact, generatedPomFile);
// try to extract POM from within the JAR
File extractedPom = null;
if (artifactItem.getExtractPom() == true
&& "jar".equals(artifactItem.getPackaging()))
{
extractedPom = extractPomFromJar(
artifactItem, stagedArtifactFile);
}

if (artifactItem.getGeneratePom() == true)
if (extractedPom != null)
{
getLog().debug(
"installing POM extracted from JAR: "
+ extractedPom.getAbsolutePath());
ArtifactMetadata pomMetadata =
new ProjectArtifactMetadata(
artifact, extractedPom);
artifact.addMetadata(pomMetadata);
generatedPomFile = extractedPom;
}
else if (artifactItem.getGeneratePom() == true)
{
// dynamically create a new POM file for
// this artifact
generatedPomFile = generatePomFile(artifactItem);
ArtifactMetadata pomMetadata =
new ProjectArtifactMetadata(
artifact, generatedPomFile);
getLog().debug(
"installing generated POM file: "
+ generatedPomFile
.getCanonicalPath());
.getAbsolutePath());
artifact.addMetadata(pomMetadata);
}
}
Expand Down
Loading