Skip to content

Commit

Permalink
Fix #480
Browse files Browse the repository at this point in the history
Signed-off-by: Clement Escoffier <clement.escoffier@gmail.com>
  • Loading branch information
cescoffier committed Apr 26, 2015
1 parent ed0814b commit c86a269
Show file tree
Hide file tree
Showing 21 changed files with 1,099 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
*/
package org.wisdom.maven.mojos;

import org.apache.maven.shared.model.fileset.FileSet;
import org.apache.maven.shared.model.fileset.util.FileSetManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
Expand All @@ -33,6 +38,10 @@ public class Aggregation {

private List<String> files = new ArrayList<>();

private static final FileSetManager FILESET_MANAGER = new FileSetManager();

private FileSet fileset;

public List<String> getFiles() {
return files;
}
Expand All @@ -56,4 +65,49 @@ public String getOutput() {
public void setOutput(String output) {
this.output = output;
}

/**
* @return the file set.
*/
public FileSet getFileset() {
return fileset;
}

/**
* Sets the file set to include in the webjar.
*
* @param fileset the file set
*/
public void setFileset(FileSet fileset) {
this.fileset = fileset;
}

/**
* @return the selected set of files.
*/
public Collection<File> getSelectedFiles(File defaultBaseDirectory) {
// Because of a symlink issue on OpenJDK, we cannot use the FileSetManager directory, because we need to
// override a method from the DirectoryScanner.
// The exception is: java.lang.ClassNotFoundException: sun/nio/fs/AixFileSystemProvider

final FileSet set = getFileset();

File base;
if (set.getDirectory() == null) {
// Set the directory if not set
set.setDirectory(defaultBaseDirectory.getAbsolutePath());
base = defaultBaseDirectory;
} else {
base = new File(set.getDirectory());
}

String[] names = FILESET_MANAGER.getIncludedFiles(set);
List<File> files = new ArrayList<>();

for (String n : names) {
files.add(new File(base, n));
}
return files;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -187,27 +188,41 @@ private void process(Aggregation aggregation) throws MojoExecutionException {
arguments.addAll(Splitter.on(" ").splitToList(cleanCssArguments));
}

for (String file : aggregation.getFiles()) {
File theFile = new File(file);
if (theFile.exists()) {
arguments.add(theFile.getAbsolutePath());
} else {
File f = new File(getInternalAssetOutputDirectory(), file);
if (!f.exists() && !f.getName().endsWith("css")) {
// Append the extension
f = new File(getInternalAssetOutputDirectory(), file + ".css");
}
for (File file : getFiles(aggregation)) {
arguments.add(file.getAbsolutePath());
}

if (!f.exists()) {
throw new MojoExecutionException("Cannot compute aggregated CSS - the '" + f.getAbsolutePath() + "'" +
" file does not exist");
}
cleancss.execute("cleancss", arguments.toArray(new String[arguments.size()]));

arguments.add(f.getAbsolutePath());
}

private Collection<File> getFiles(Aggregation aggregation) throws MojoExecutionException {
List<File> files = new ArrayList<>();
if (aggregation.getFiles() != null && !aggregation.getFiles().isEmpty()) {
for (String file : aggregation.getFiles()) {
File theFile = new File(file);
if (theFile.exists()) {
files.add(theFile);
} else {
File f = new File(getInternalAssetOutputDirectory(), file);
if (!f.exists() && !f.getName().endsWith("css")) {
// Append the extension
f = new File(getInternalAssetOutputDirectory(), file + ".css");
}

if (!f.exists()) {
throw new MojoExecutionException("Cannot compute aggregated CSS - the '" + f.getAbsolutePath() + "'" +
" file does not exist");
}
files.add(f);
}
}
return files;
}

cleancss.execute("cleancss", arguments.toArray(new String[arguments.size()]));
// Else it uses a file set, it must indicate whether or not the file is in the output directory
return aggregation.getSelectedFiles(getInternalAssetOutputDirectory());


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,8 @@ private void compile(Aggregation aggregation) throws WatchingException {
}

List<SourceFile> inputs = new ArrayList<>();
for (String file : aggregation.getFiles()) {
File theFile = new File(file);
if (theFile.exists()) {
inputs.add(SourceFile.fromFile(theFile));
} else {
File f = new File(getInternalAssetOutputDirectory(), file);
if (!f.exists() && !f.getName().endsWith("js")) {
// Append the extension
f = new File(getInternalAssetOutputDirectory(), file + ".js");
}

if (!f.exists()) {
throw new WatchingException("Cannot compute aggregated JavaScript - the '"
+ f.getAbsolutePath() + "' file does not exist");
}

inputs.add(SourceFile.fromFile(f));
}
for (File file : getFiles(aggregation)) {
inputs.add(SourceFile.fromFile(file));
}


Expand Down Expand Up @@ -231,6 +215,35 @@ private void compile(Aggregation aggregation) throws WatchingException {
}
}

private Collection<File> getFiles(Aggregation aggregation) throws WatchingException {
List<File> list = new ArrayList<>();

if (aggregation.getFiles() != null && ! aggregation.getFiles().isEmpty()) {
for (String file : aggregation.getFiles()) {
File theFile = new File(file);
if (theFile.exists()) {
list.add(theFile);
} else {
File f = new File(getInternalAssetOutputDirectory(), file);
if (!f.exists() && !f.getName().endsWith("js")) {
// Append the extension
f = new File(getInternalAssetOutputDirectory(), file + ".js");
}

if (!f.exists()) {
throw new WatchingException("Cannot compute aggregated JavaScript - the '"
+ f.getAbsolutePath() + "' file does not exist");
}
list.add(f);
}
}
return list;
}

// Else we use a file set.
return aggregation.getSelectedFiles(getInternalAssetOutputDirectory());
}

private File fixPath(File output) {
if (output.isAbsolute()) {
return output;
Expand Down
67 changes: 67 additions & 0 deletions documentation/documentation/src/main/resources/assets/asset.ad
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ assets (i.e. from `src/main/resources/assets`) can be aggregated. Each files is
`target/classes/assets`. By default it builds the name as follows: _artifactid-min.css_ (or just _artifactid.css_ if
minification is disabled).

If you don't want to list all the file one by one, you can use a `FileSet` instead of `<files>`:

[source, xml]
----
<fileset>
<includes>
<include>stylesheets/*/*.css</include>
<include>less/*.css</include>
</includes>
</fileset>
----

Be aware that wildcards can introduce ordering issue as the order of the selected files may depend on the file system
you are using.

Using a file set let you exclude files too, as well as set the base directory. By default, the directory is the
location of internal assets (`target/classes/assets`):

[source, xml]
----
<fileset>
<directory>target/external</directory>
<exludes>
<exclude>**/*.map</exclude>
</excludes>
<includes>
<include>*.css</include>
</includes>
</fileset>
----

IMPORTANT: if `files` is set, the `fileset` is ignored.

=== JavaScript Aggregation and Minification

Wisdom integrates https://developers.google.com/closure/[Google Closure] to check, aggregate and minify JavaScript
Expand Down Expand Up @@ -214,6 +247,40 @@ looked up from the `target/classes/assets` directory (containing the compiles as
(for example, `coffee/math` instead of `coffee/math.js`). The aggregation can optionally defined the output file, also
related to `target/classes/assets`. By default it builds the name as follows: _artifactid-min.js_.


If you don't want to list all the file one by one, you can use a `FileSet` instead of `<files>`:

[source, xml]
----
<fileset>
<includes>
<include>coffee/*.js</include>
<include>js/*.js</include>
</includes>
</fileset>
----

Be aware that wildcards can introduce ordering issue as the order of the selected files may depend on the file system
you are using.

Using a file set let you exclude files too, as well as set the base directory. By default, the directory is the
location of internal assets (`target/classes/assets`):

[source, xml]
----
<fileset>
<directory>target/external</directory>
<exludes>
<exclude>**/*.map</exclude>
</excludes>
<includes>
<include>*.js</include>
</includes>
</fileset>
----

IMPORTANT: if `files` is set, the `fileset` is ignored.

You can also disable the Google Closure support with:

[source, xml]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean verify
Loading

0 comments on commit c86a269

Please sign in to comment.