[Arakhnę-Dev] [366] * Make tag-replacement faster. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 366
Author: galland
Date: 2012-08-13 13:36:14 +0200 (Mon, 13 Aug 2012)
Log Message:
-----------
* Make tag-replacement faster.
Modified Paths:
--------------
trunk/pom.xml
trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/AbstractReplaceMojo.java
trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/GenerateSourceMojo.java
trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceHtmlMojo.java
trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceResourceMojo.java
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2012-07-11 21:08:55 UTC (rev 365)
+++ trunk/pom.xml 2012-08-13 11:36:14 UTC (rev 366)
@@ -359,6 +359,19 @@
<ignore />
</action>
</pluginExecution>
+ <pluginExecution>
+ <pluginExecutionFilter>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <versionRange>[0.1,)</versionRange>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ </pluginExecutionFilter>
+ <action>
+ <ignore />
+ </action>
+ </pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
Modified: trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/AbstractReplaceMojo.java
===================================================================
--- trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/AbstractReplaceMojo.java 2012-07-11 21:08:55 UTC (rev 365)
+++ trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/AbstractReplaceMojo.java 2012-08-13 11:36:14 UTC (rev 366)
@@ -37,6 +37,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
+import java.util.Set;
+import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -251,6 +253,8 @@
*/
private List<RemoteRepository> remoteRepos;
+ private final Set<File> replacementTreatedFiles = new TreeSet<File>();
+
private String ensureArtifactId(ExtendedArtifact artifact) {
if (artifact != null) {
if (this.overrideArtifactGroup && this.mavenProject.getArtifact().equals(artifact)) {
@@ -278,6 +282,32 @@
}
/**
+ * Replace the Javadoc tags in the given file only if the file was never
+ * treated before.
+ *
+ * @param sourceFile
+ * is the name of the file to read out. It may be <code>null</code>
+ * @param targetFile
+ * is the name of the file to write in. It cannot be <code>null</code>
+ * @param replacementType
+ * is the type of replacement to be done.
+ * @param classpath
+ * are the directories from which the file is extracted.
+ * @param detectEncoding
+ * when <code>true</code> the encoding of the file will be detected and preserved. When <code>false</code> the encoding may be loose.
+ * @throws MojoExecutionException
+ * @see {@link #replaceInFile(File, File, ReplacementType, File[], boolean)}
+ */
+ protected synchronized void replaceInFileBuffered(File sourceFile, File targetFile, ReplacementType replacementType, File[] classpath, boolean detectEncoding) throws MojoExecutionException {
+ if (this.replacementTreatedFiles.contains(targetFile)
+ && targetFile.exists()) {
+ getLog().debug("Skiping "+targetFile+" because is was already treated for replacements"); //$NON-NLS-1$//$NON-NLS-2$
+ return;
+ }
+ replaceInFile(sourceFile, targetFile, replacementType, classpath, detectEncoding);
+ }
+
+ /**
* Replace the Javadoc tags in the given file.
*
* @param sourceFile
@@ -291,6 +321,7 @@
* @param detectEncoding
* when <code>true</code> the encoding of the file will be detected and preserved. When <code>false</code> the encoding may be loose.
* @throws MojoExecutionException
+ * @see {@link #replaceInFileBuffered(File, File, ReplacementType, File[], boolean)}
*/
protected synchronized void replaceInFile(File sourceFile, File targetFile, ReplacementType replacementType, File[] classpath, boolean detectEncoding) throws MojoExecutionException {
File outputFile, inputFile;
@@ -644,6 +675,9 @@
* @throws MojoExecutionException
*/
protected synchronized String replaceInString(File sourceFile, int sourceLine, String file, ExtendedArtifact artifact, String line, ReplacementType replacementType) throws MojoExecutionException {
+ // No possibility of replacement
+ if (!line.contains("$")) return line; //$NON-NLS-1$
+
String nline = line;
String replacementName = (artifact == null) ? null : artifact.getName();
Modified: trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/GenerateSourceMojo.java
===================================================================
--- trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/GenerateSourceMojo.java 2012-07-11 21:08:55 UTC (rev 365)
+++ trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/GenerateSourceMojo.java 2012-08-13 11:36:14 UTC (rev 366)
@@ -18,14 +18,16 @@
*/
package org.arakhne.maven.plugins.tagreplacer;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.arakhne.maven.JavaSourceFileFilter;
-
import java.io.File;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
import java.util.TreeMap;
-import java.util.Map.Entry;
+import java.util.TreeSet;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.arakhne.maven.JavaSourceFileFilter;
+
/**
* Generate the Java source files and replace the macros by the corresponding values
* on the fly.
@@ -57,6 +59,15 @@
*/
private File outputLocation;
+ /** Set of the files generated by the plugin.
+ * This set permits to not generate the file more than one time.
+ */
+ private final Set<File> generateTargets = new TreeSet<File>();
+
+ /** Set of the files inside the file system.
+ */
+ private final Map<File,Map<File,File>> bufferedFiles = new TreeMap<File,Map<File,File>>();
+
/**
* {@inheritDoc}
*/
@@ -80,6 +91,14 @@
* @throws MojoExecutionException
*/
protected synchronized void executeMojo(File targetDir) throws MojoExecutionException {
+ if (this.generateTargets.contains(targetDir) &&
+ targetDir.isDirectory()) {
+ getLog().debug("Skiping "+targetDir+" because is was already generated"); //$NON-NLS-1$//$NON-NLS-2$
+ return;
+ }
+
+ this.generateTargets.add(targetDir);
+
File[] sourceDirs;
File mainDirectory = new File(getSourceDirectory(), "main"); //$NON-NLS-1$
if (this.sources==null || this.sources.length==0) {
@@ -93,23 +112,28 @@
clearInternalBuffers();
- Map<File,File> htmlBasedFiles = new TreeMap<File,File>();
-
for(File sourceDir : sourceDirs) {
- if ( sourceDir.isDirectory() ) {
- // Search for .java files
- findFiles(sourceDir, new JavaSourceFileFilter(), htmlBasedFiles);
+ Map<File,File> htmlBasedFiles = this.bufferedFiles.get(sourceDir);
+
+ if (htmlBasedFiles==null) {
+ htmlBasedFiles = new TreeMap<File,File>();
+ if ( sourceDir.isDirectory() ) {
+ // Search for .java files
+ findFiles(sourceDir, new JavaSourceFileFilter(), htmlBasedFiles);
+ }
+
+ this.bufferedFiles.put(sourceDir, htmlBasedFiles);
}
- }
- for(Entry<File,File> entry : htmlBasedFiles.entrySet()) {
- String baseFile = removePathPrefix(entry.getValue(), entry.getKey());
- replaceInFile(
- entry.getKey(),
- new File(targetDir, baseFile),
- ReplacementType.HTML,
- sourceDirs,
- false);
+ for(Entry<File,File> entry : htmlBasedFiles.entrySet()) {
+ String baseFile = removePathPrefix(entry.getValue(), entry.getKey());
+ replaceInFileBuffered(
+ entry.getKey(),
+ new File(targetDir, baseFile),
+ ReplacementType.HTML,
+ sourceDirs,
+ false);
+ }
}
}
Modified: trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceHtmlMojo.java
===================================================================
--- trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceHtmlMojo.java 2012-07-11 21:08:55 UTC (rev 365)
+++ trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceHtmlMojo.java 2012-08-13 11:36:14 UTC (rev 366)
@@ -24,6 +24,8 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
/**
* Replace the macros by the corresponding values
@@ -50,6 +52,10 @@
*/
private File[] htmls;
+ /** Set of the files inside the file system.
+ */
+ private final Map<File,Collection<File>> bufferedFiles = new TreeMap<File,Collection<File>>();
+
/**
* {@inheritDoc}
*/
@@ -67,22 +73,25 @@
clearInternalBuffers();
- Collection<File> textBasedFiles = new ArrayList<File>();
for(File htmlDir : htmlDirs) {
- if ( htmlDir.isDirectory() ) {
- // Search for .html files
- findFiles(htmlDir, new PropertyFileFilter(), textBasedFiles);
- }
+ Collection<File> textBasedFiles = this.bufferedFiles.get(htmlDir);
+ if (textBasedFiles==null) {
+ textBasedFiles = new ArrayList<File>();
+ if ( htmlDir.isDirectory() ) {
+ // Search for .html files
+ findFiles(htmlDir, new PropertyFileFilter(), textBasedFiles);
+ }
+ this.bufferedFiles.put(htmlDir, textBasedFiles);
+ }
+ for(File file : textBasedFiles) {
+ replaceInFileBuffered(
+ null,
+ file,
+ ReplacementType.HTML,
+ htmlDirs,
+ true);
+ }
}
-
- for(File file : textBasedFiles) {
- replaceInFile(
- null,
- file,
- ReplacementType.HTML,
- htmlDirs,
- true);
- }
}
}
Modified: trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceResourceMojo.java
===================================================================
--- trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceResourceMojo.java 2012-07-11 21:08:55 UTC (rev 365)
+++ trunk/tag-replacer/src/main/java/org/arakhne/maven/plugins/tagreplacer/ReplaceResourceMojo.java 2012-08-13 11:36:14 UTC (rev 366)
@@ -24,6 +24,8 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
/**
* Generate the property files and replace the macros by the corresponding values
@@ -49,6 +51,10 @@
*/
private File[] sources;
+ /** Set of the files inside the file system.
+ */
+ private final Map<File,Collection<File>> bufferedFiles = new TreeMap<File,Collection<File>>();
+
/**
* {@inheritDoc}
*/
@@ -66,23 +72,26 @@
clearInternalBuffers();
- Collection<File> textBasedFiles = new ArrayList<File>();
for(File sourceDir : sourceDirs) {
- if ( sourceDir.isDirectory() ) {
- // Search for .properties files
- findFiles(sourceDir, new PropertyFileFilter(), textBasedFiles);
- }
+ Collection<File> textBasedFiles = this.bufferedFiles.get(sourceDir);
+ if (textBasedFiles==null) {
+ textBasedFiles = new ArrayList<File>();
+ if ( sourceDir.isDirectory() ) {
+ // Search for .properties files
+ findFiles(sourceDir, new PropertyFileFilter(), textBasedFiles);
+ }
+ this.bufferedFiles.put(sourceDir, textBasedFiles);
+ }
+ for(File file : textBasedFiles) {
+ replaceInFileBuffered(
+ null,
+ file,
+ ReplacementType.TEXT,
+ sourceDirs,
+ true);
+ }
}
-
- for(File file : textBasedFiles) {
- replaceInFile(
- null,
- file,
- ReplacementType.TEXT,
- sourceDirs,
- true);
- }
}
}