Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	pom.xml
  • Loading branch information
agabrys committed Nov 4, 2016
2 parents 147675a + 17c4ee9 commit a7bdf51
Show file tree
Hide file tree
Showing 25 changed files with 584 additions and 93 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Maven Plugin Utils Changelog

## 1.4.0
Features:
* Added [ContextClassLoaderExtender](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/classpath/ContextClassLoaderExtender.html) responsible for adding Maven project dependencies to classpath
* Added [ParameterUtils](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/parameter/ParameterUtils.html) which provides tools to work with method parameters
* Separation of the seconds and milliseconds in the [Time#toString()](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/apidocs/index.html?biz/gabrys/maven/plugin/util/timer/Time.html) method

[See documentation](http://maven-plugin-utils.projects.gabrys.biz/1.4.0/)

## 1.3.0
Features:
* Added [ParametersLogBuilder](http://maven-plugin-utils.projects.gabrys.biz/1.3.0/apidocs/index.html?biz/gabrys/maven/plugin/util/parameter/ParametersLogBuilder.html) which allows to log Mojos parameters values
Expand Down
17 changes: 9 additions & 8 deletions README.md
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).
16 changes: 11 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

<groupId>biz.gabrys.maven</groupId>
<artifactId>plugin-utils</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
<name>Maven Plugin Utils</name>
<description>A Java library which contains utilities for Maven 2 plugins.</description>
<url>http://maven-plugin-utils.projects.gabrys.biz/1.3.0/</url>
<url>http://maven-plugin-utils.projects.gabrys.biz/1.4.0/</url>

<inceptionYear>2015</inceptionYear>
<organization>
Expand Down Expand Up @@ -72,6 +72,12 @@
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down Expand Up @@ -113,7 +119,7 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.6.0</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
Expand All @@ -138,7 +144,7 @@
<links>
<link>https://maven.apache.org/ref/2.0.11/apidocs</link>
<link>http://codehaus-plexus.github.io/plexus-utils/apidocs</link>
<link>http://commons.apache.org/proper/commons-io/javadocs/api-2.2</link>
<link>https://commons.apache.org/proper/commons-io/javadocs/api-2.2</link>
</links>
</configuration>
</plugin>
Expand Down Expand Up @@ -169,7 +175,7 @@
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.11</version>
<version>3.0</version>
<configuration>
<header>header-license-template.txt</header>
<properties>
Expand Down
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());
}
}
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;
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Collection<File> getFiles(final File directory, final String[] includes,
}

/**
* Creates a {@link DirectoryScanner} which will be used to find all files which match filters.
* Creates a {@link DirectoryScanner} which will be used to find all files whose match filters.
* @return a not initialized instance of the {@link DirectoryScanner} or its subclass.
* @since 1.3.0
* @see #getFiles(File, String[], String[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* <p>
* Responsible for scanning a directory for files which match certain criteria.
* Responsible for scanning a directory for files whose match certain criteria.
* </p>
* <p>
* Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.DirectoryScanner;

import biz.gabrys.maven.plugin.util.parameter.ParameterUtils;

/**
* Extended version of the {@link DirectoryScanner} which adds logger instructions in debug mode.
* @since 1.2
Expand All @@ -32,9 +34,7 @@ public class LoggingDirectoryScanner extends DirectoryScanner {
* @since 1.2
*/
public LoggingDirectoryScanner(final Log logger) {
if (logger == null) {
throw new IllegalArgumentException("Logger cannot be null");
}
ParameterUtils.verifyNotNull("logger", logger);
this.logger = logger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.maven.plugin.logging.Log;

import biz.gabrys.maven.plugin.util.parameter.ParameterUtils;

/**
* Decorator which adds logger instructions (in debug mode) to an instance of the {@link IOFileFilter}.
* @since 1.2
Expand All @@ -34,12 +36,9 @@ public class LoggingFileFilter implements IOFileFilter {
* @since 1.2
*/
public LoggingFileFilter(final IOFileFilter filter, final Log logger) {
if (filter == null) {
throw new IllegalArgumentException("Filter cannot be null");
}
if (logger == null) {
throw new IllegalArgumentException("Logger cannot be null");
}
ParameterUtils.verifyNotNull("filter", filter);
ParameterUtils.verifyNotNull("logger", logger);

this.filter = filter;
this.logger = logger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Collection<File> getFiles(final File directory, final String[] includes,
}

/**
* Creates a {@link IOFileFilter} which will be used to find all files which match filters.
* Creates a {@link IOFileFilter} which will be used to find all files whose match filters.
* @param directory the directory to be scanned.
* @param includes an array of include patterns.
* @param excludes an array of exclude patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import org.apache.maven.plugin.logging.Log;

import biz.gabrys.maven.plugin.util.parameter.ParameterUtils;

/**
* Responsible for creating new instances of the {@link FileScanner} by the {@link ScannerPatternFormat}.
* @since 1.0
Expand All @@ -38,9 +40,7 @@ public ScannerFactory() {
* @since 1.0
*/
public FileScanner create(final ScannerPatternFormat patternFormat, final Log logger) {
if (logger == null) {
throw new IllegalArgumentException("Logger cannot be null");
}
ParameterUtils.verifyNotNull("logger", logger);
if (patternFormat == ScannerPatternFormat.ANT) {
return new AntFileScanner(logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Contains classes related with I/O operations. It provides tools to:
* </p>
* <ul>
* <li>scan directories for files which match certain criteria (see {@link biz.gabrys.maven.plugin.util.io.FileScanner
* <li>scan directories for files whose match certain criteria (see {@link biz.gabrys.maven.plugin.util.io.FileScanner
* FileScanner})</li>
* <li>create a virtual (not physical) destination file in the output directory based on the source directory, the
* source file and the output file name pattern (see {@link biz.gabrys.maven.plugin.util.io.DestinationFileCreator
Expand Down
Loading

0 comments on commit a7bdf51

Please sign in to comment.