This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # README.md # pom.xml
- Loading branch information
Showing
25 changed files
with
584 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
# About Maven Plugin Utils | ||
# About | ||
[![License BSD 3-Clause](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg)](http://maven-plugin-utils.projects.gabrys.biz/license.txt) | ||
[![Build Status](https://travis-ci.org/gabrysbiz/maven-plugin-utils.svg?branch=master)](https://travis-ci.org/gabrysbiz/maven-plugin-utils) | ||
|
||
Maven Plugin Utils is a Java library which contains utilities for [Maven 2](https://maven.apache.org/) plugins. It contains tools to: | ||
* scan directories for files and create a virtual (not physical) destination file in the output directory based on the source (see [biz.gabrys.maven.plugin.util.io](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/apidocs/index.html?biz/gabrys/maven/plugin/util/io/package-summary.html) package) | ||
* log Mojos parameters (see [biz.gabrys.maven.plugin.util.parameter](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/apidocs/index.html?biz/gabrys/maven/plugin/util/parameter/package-summary.html) package) | ||
* count execution time (see [biz.gabrys.maven.plugin.util.timer](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/apidocs/index.html?biz/gabrys/maven/plugin/util/timer/package-summary.html) package) | ||
* scan directories for files and create a virtual (not physical) destination file in the output directory based on the source (see [biz.gabrys.maven.plugin.util.io](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/io/package-summary.html) package) | ||
* log Mojos parameters (see [biz.gabrys.maven.plugin.util.parameter](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/parameter/package-summary.html) package) | ||
* manage classpath (see [biz.gabrys.maven.plugin.util.classpath](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/classpath/package-summary.html) package) | ||
* count execution time (see [biz.gabrys.maven.plugin.util.timer](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/timer/package-summary.html) package) | ||
|
||
Read [API documentation](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/apidocs/) for more information. | ||
Read [API documentation](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/) for more information. | ||
|
||
# Requirements | ||
The library to run requires: | ||
* Java 5.0 or higher | ||
* Third-Party Dependencies ([see list](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/dependencies.html)) | ||
* Third-Party Dependencies ([see list](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/dependencies.html)) | ||
|
||
# Download | ||
You can download the library from [this page](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/download.html) | ||
or using various [dependency management tools](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/dependency-info.html). | ||
You can download the library from [this page](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/download.html) | ||
or using various [dependency management tools](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/dependency-info.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/biz/gabrys/maven/plugin/util/classpath/ContextClassLoaderExtender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Maven Plugin Utils | ||
* http://maven-plugin-utils.projects.gabrys.biz/ | ||
* | ||
* Copyright (c) 2015 Adam Gabryś | ||
* | ||
* This file is licensed under the BSD 3-Clause (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain: | ||
* - a copy of the License at project page | ||
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
package biz.gabrys.maven.plugin.util.classpath; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.artifact.Artifact; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.apache.maven.project.MavenProject; | ||
|
||
import biz.gabrys.maven.plugin.util.parameter.ParameterUtils; | ||
|
||
/** | ||
* Responsible for adding Maven project dependencies to classpath. | ||
* @since 1.4.0 | ||
*/ | ||
public class ContextClassLoaderExtender { | ||
|
||
private final MavenProject project; | ||
private final Log logger; | ||
|
||
/** | ||
* Creates a new instance. | ||
* @param project the Maven project. | ||
* @param logger the logger. | ||
* @throws IllegalArgumentException if the Maven project or/and the logger is equal to {@code null}. | ||
* @since 1.4.0 | ||
*/ | ||
public ContextClassLoaderExtender(final MavenProject project, final Log logger) { | ||
ParameterUtils.verifyNotNull("project", project); | ||
ParameterUtils.verifyNotNull("logger", logger); | ||
this.project = project; | ||
this.logger = logger; | ||
} | ||
|
||
/** | ||
* Adds all dependencies with specified types to context class loader. | ||
* @param types the supported types. | ||
* @throws IllegalArgumentException if the types are equal to {@code null}. | ||
* @since 1.4.0 | ||
*/ | ||
public void addDependencies(final String... types) { | ||
ParameterUtils.verifyNotNull("types", types); | ||
addDependencies(new HashSet<String>(Arrays.asList(types))); | ||
} | ||
|
||
/** | ||
* Adds all dependencies with specified types to context class loader. | ||
* @param types the supported types. | ||
* @throws IllegalArgumentException if the types collection is equal to {@code null}. | ||
* @since 1.4.0 | ||
*/ | ||
public void addDependencies(final Collection<String> types) { | ||
ParameterUtils.verifyNotNull("types", types); | ||
@SuppressWarnings("unchecked") | ||
final Set<Artifact> artifacts = project.getArtifacts(); | ||
final List<Artifact> filtered = filterArtifacts(artifacts, types); | ||
final List<URL> urls = resolveArtifactsUrls(filtered); | ||
addToContextClassLoader(urls); | ||
} | ||
|
||
/** | ||
* Filters artifacts based on the type. | ||
* @param artifacts the collection which stores artifacts. | ||
* @param types the supported types. | ||
* @return the list with artifacts whose types fit to the supported types. | ||
* @since 1.4.0 | ||
*/ | ||
protected List<Artifact> filterArtifacts(final Collection<Artifact> artifacts, final Collection<String> types) { | ||
final List<Artifact> filtered = new LinkedList<Artifact>(); | ||
for (final Artifact artifact : artifacts) { | ||
if (types.contains(artifact.getType())) { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug(String.format("Include %s", createDisplayText(artifact))); | ||
} | ||
filtered.add(artifact); | ||
} else if (logger.isDebugEnabled()) { | ||
logger.debug(String.format("Exclude %s", createDisplayText(artifact))); | ||
} | ||
} | ||
return filtered; | ||
} | ||
|
||
/** | ||
* Returns URLs whose represents artifacts. | ||
* @param artifacts the collection which stores artifacts. | ||
* @return the artifacts' URLs. | ||
* @since 1.4.0 | ||
*/ | ||
protected List<URL> resolveArtifactsUrls(final Collection<Artifact> artifacts) { | ||
final List<URL> urls = new ArrayList<URL>(artifacts.size()); | ||
for (final Artifact artifact : artifacts) { | ||
try { | ||
urls.add(artifact.getFile().toURI().toURL()); | ||
} catch (final MalformedURLException e) { | ||
// never | ||
throw new IllegalStateException(String.format("Cannot add %s to the classpath!", createDisplayText(artifact)), e); | ||
} | ||
} | ||
return urls; | ||
} | ||
|
||
/** | ||
* Adds artifacts URLs to context class loader. | ||
* @param urls the artifacts URLs. | ||
* @since 1.4.0 | ||
*/ | ||
protected void addToContextClassLoader(final List<URL> urls) { | ||
final Thread currentThread = Thread.currentThread(); | ||
final URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[0]), currentThread.getContextClassLoader()); | ||
currentThread.setContextClassLoader(classLoader); | ||
} | ||
|
||
/** | ||
* Creates a text representation of the {@link Artifact}. | ||
* @param artifact the artifact. | ||
* @return the text representation of the {@link Artifact}. | ||
* @since 1.4.0 | ||
*/ | ||
protected String createDisplayText(final Artifact artifact) { | ||
return String.format("%s:%s-%s.%s (%s)", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), | ||
artifact.getScope()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/biz/gabrys/maven/plugin/util/classpath/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Maven Plugin Utils | ||
* http://maven-plugin-utils.projects.gabrys.biz/ | ||
* | ||
* Copyright (c) 2015 Adam Gabryś | ||
* | ||
* This file is licensed under the BSD 3-Clause (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain: | ||
* - a copy of the License at project page | ||
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
/** | ||
* <p> | ||
* Contains tools to operate on classpath. | ||
* </p> | ||
*/ | ||
package biz.gabrys.maven.plugin.util.classpath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.