Author: desruisseaux
Date: Tue Jan  1 17:59:06 2013
New Revision: 1427505

URL: http://svn.apache.org/viewvc?rev=1427505&view=rev
Log:
Ported JarCollector in sis-build-helper, in order to simplify usage of the new 
About class.
On the JDK7 branch this use hard links, so the cost should be low. For the JDK6 
branch, the
strategy is yet to be determined.

Added:
    
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/
    
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java
   (with props)
    
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java
   (with props)
Modified:
    sis/branches/JDK7/pom.xml
    sis/branches/JDK7/sis-build-helper/src/site/apt/index.apt
    sis/branches/JDK7/sis-utility/pom.xml
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java

Modified: sis/branches/JDK7/pom.xml
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/pom.xml?rev=1427505&r1=1427504&r2=1427505&view=diff
==============================================================================
--- sis/branches/JDK7/pom.xml (original)
+++ sis/branches/JDK7/pom.xml Tue Jan  1 17:59:06 2013
@@ -274,17 +274,21 @@ Apache SIS is a toolkit for describing l
 
 
 
-  <!-- ==============================================================
-         Properties, used in dependency management and build
-         configuration.
-       ============================================================== -->
+  <!-- ===================================================================
+         Properties used in dependency management and build configuration.
+         The last properties in this list depend on the Apache SIS branch.
+       =================================================================== -->
   <properties>
     <geoapi.version>3.1-M04</geoapi.version>
-    <maven.compile.source>1.7</maven.compile.source>
-    <maven.compile.target>1.7</maven.compile.target>
+    <findbugs.version>2.5.2</findbugs.version>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <website.encoding>UTF-8</website.encoding>
     <website.locale>en</website.locale>
+
+    <!-- Following properties are branch-specific -->
+    <maven.compile.source>1.7</maven.compile.source>
+    <maven.compile.target>1.7</maven.compile.target>
+    <sis.plugin.version>${project.version}</sis.plugin.version>
   </properties>
 
   <profiles>
@@ -423,12 +427,12 @@ Apache SIS is a toolkit for describing l
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>findbugs-maven-plugin</artifactId>
-        <version>2.5.2</version>
+        <version>${findbugs.version}</version>
         <dependencies>
           <dependency>
             <groupId>org.apache.sis</groupId>
             <artifactId>sis-build-helper</artifactId>
-            <version>${project.version}</version>
+            <version>${sis.plugin.version}</version>
           </dependency>
         </dependencies>
       </plugin>
@@ -529,7 +533,7 @@ Apache SIS is a toolkit for describing l
           <tagletArtifact>
             <groupId>org.apache.sis</groupId>
             <artifactId>sis-build-helper</artifactId>
-            <version>${project.version}</version>
+            <version>${sis.plugin.version}</version>
           </tagletArtifact>
         </configuration>
       </plugin>
@@ -559,7 +563,7 @@ Apache SIS is a toolkit for describing l
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>findbugs-maven-plugin</artifactId>
-        <version>2.5.2</version> <!-- See also the same plugin in the <build> 
section. -->
+        <version>${findbugs.version}</version> <!-- See also the same plugin 
in the <build> section. -->
         <configuration>
           <threshold>Normal</threshold>
           <effort>Default</effort>

Added: 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java?rev=1427505&view=auto
==============================================================================
--- 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java
 (added)
+++ 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java
 Tue Jan  1 17:59:06 2013
@@ -0,0 +1,231 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.maven;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Files;
+import java.util.Set;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.artifact.Artifact;
+
+
+/**
+ * Copies <code>.jar</code> files in a single "{@code target/binaries}" 
directory.
+ * Dependencies are copied as well, except if already presents.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-2.2)
+ * @version 0.3
+ * @module
+ *
+ * @goal collect-jars
+ * @phase package
+ * @requiresDependencyResolution runtime
+ */
+public final class JarCollector extends AbstractMojo implements FileFilter {
+    /**
+     * The sub directory to create inside the "target" directory.
+     */
+    static final String SUB_DIRECTORY = "binaries";
+
+    /**
+     * The Maven project running this plugin.
+     *
+     * @parameter property="project"
+     * @required
+     */
+    private MavenProject project;
+
+    /**
+     * The target directory (without the "<code>binaries</code>" 
sub-directory) where JARs are
+     * to be copied. It should be the "target" directory of the parent 
<code>pom.xml</code>.
+     *
+     * @parameter property="project.parent.build.directory"
+     * @required
+     */
+    private String collectDirectory;
+
+    /**
+     * Copies the {@code *.jar} files to the collect directory.
+     *
+     * @throws MojoExecutionException if the plugin execution failed.
+     */
+    @Override
+    public void execute() throws MojoExecutionException {
+        if (collectDirectory == null || collectDirectory.startsWith("${")) {
+            getLog().warn("Unresolved directory: " + collectDirectory);
+            return;
+        }
+        /*
+         * Now collects the JARs.
+         */
+        try {
+            collect();
+        } catch (IOException e) {
+            throw new MojoExecutionException("Error collecting the JAR 
files.", e);
+        }
+    }
+
+    /**
+     * Implementation of the {@link #execute()} method.
+     */
+    private void collect() throws MojoExecutionException, IOException {
+        /*
+         * Make sure that we are collecting the JAR file from a module which 
produced
+         * such file. Some modules use pom packaging, which do not produce any 
JAR file.
+         */
+        final File jarFile = getProjectFile();
+        if (jarFile == null) {
+            return;
+        }
+        /*
+         * Get the "target" directory of the parent pom.xml and make sure it 
exists.
+         */
+        File collect = new File(collectDirectory);
+        if (!collect.exists()) {
+            if (!collect.mkdir()) {
+                throw new MojoExecutionException("Failed to create target 
directory.");
+            }
+        }
+        if 
(collect.getCanonicalFile().equals(jarFile.getParentFile().getCanonicalFile())) 
{
+            /*
+             * The parent's directory is the same one than this module's 
directory.
+             * In other words, this plugin is not executed from the parent 
POM. Do
+             * not copy anything, since this is not the place where we want to
+             * collect the JAR files.
+             */
+            return;
+        }
+        /*
+         * Creates a "binaries" subdirectory inside the "target" directory, 
then copy the
+         * JAR file compiled by Maven. If an JAR file already existed, it will 
be deleted.
+         */
+        collect = new File(collect, SUB_DIRECTORY);
+        if (!collect.exists()) {
+            if (!collect.mkdir()) {
+                throw new MojoExecutionException("Failed to create binaries 
directory.");
+            }
+        }
+        File copy = new File(collect, jarFile.getName());
+        copy.delete();
+        copyFileToDirectory(jarFile, copy);
+        /*
+         * Copies the dependencies.
+         */
+        final Set<Artifact> dependencies = project.getArtifacts();
+        if (dependencies != null) {
+            for (final Artifact artifact : dependencies) {
+                final String scope = artifact.getScope();
+                if (scope != null &&  // Maven 2.0.6 bug?
+                   (scope.equalsIgnoreCase(Artifact.SCOPE_COMPILE) ||
+                    scope.equalsIgnoreCase(Artifact.SCOPE_RUNTIME)))
+                {
+                    final File file = artifact.getFile();
+                    if (file != null) { // I'm not sure why the file is 
sometime null...
+                        copy = new File(collect, getFinalName(file, artifact));
+                        if (!copy.exists()) {
+                            /*
+                             * Copies the dependency only if it was not 
already copied. Note that
+                             * the module's JAR was copied unconditionally 
above (because it may
+                             * be the result of a new compilation).
+                             */
+                            copyFileToDirectory(file, copy);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Filters the content of the "target" directory in order to keep only the 
project
+     * build result. We scan the directory because the final name may be 
different than
+     * the actual file name, because a classifier may have been added to the 
name.
+     *
+     * <p>The {@code .jar} extension is not quite appropriate for source and 
Javadoc files;
+     * a better extension would be {@code .zip}. Unfortunately the {@code 
.jar} extension
+     * for those content is a very common practice, so we have to filter 
them.</p>
+     */
+    @Override
+    public boolean accept(final File pathname) {
+        final String name = pathname.getName();
+        return name.endsWith(".jar")         &&
+              !name.endsWith("-tests.jar")   &&
+              !name.endsWith("-sources.jar") &&
+              !name.endsWith("-javadoc.jar") &&
+               name.startsWith(project.getBuild().getFinalName()) &&
+               pathname.isFile();
+    }
+
+    /**
+     * Returns the JAR file, or {@code null} if none.
+     * In case of doubt, conservatively returns {@code null}.
+     */
+    private File getProjectFile() {
+        final File[] files = new 
File(project.getBuild().getDirectory()).listFiles(this);
+        if (files != null && files.length == 1) {
+            return files[0];
+        }
+        return null;
+    }
+
+    /**
+     * Returns the name of the given file. If the given file is a snapshot, 
then the
+     * {@code "SNAPSHOT"} will be replaced by the timestamp if possible.
+     *
+     * @param  file     The file from which to get the filename.
+     * @param  artifact The artifact that produced the given file.
+     * @return The filename to use.
+     */
+    private static String getFinalName(final File file, final Artifact 
artifact) {
+        String filename = file.getName();
+        final String baseVersion = artifact.getBaseVersion();
+        if (baseVersion != null) {
+            final int pos = filename.lastIndexOf(baseVersion);
+            if (pos >= 0) {
+                final String version = artifact.getVersion();
+                if (version != null && !baseVersion.equals(version)) {
+                    filename = filename.substring(0, pos) + version + 
filename.substring(pos + baseVersion.length());
+                }
+            }
+        }
+        return filename;
+    }
+
+    /**
+     * Copies the given file to the given target file.
+     * Since JDK 7, this method actually creates a hard link.
+     *
+     * @param file The source file to read.
+     * @param copy The destination file to create.
+     */
+    private static void copyFileToDirectory(final File file, final File copy) 
throws IOException {
+        final Path source = file.toPath();
+        final Path target = copy.toPath();
+        try {
+            Files.createLink(target, source);
+        } catch (UnsupportedOperationException e) {
+            // If hard links are not supported, do a plain copy.
+            Files.copy(source, target);
+        }
+    }
+}

Propchange: 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/JarCollector.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java?rev=1427505&view=auto
==============================================================================
--- 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java
 (added)
+++ 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java
 Tue Jan  1 17:59:06 2013
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+/**
+ * Maven plugins (others than {@link 
org.apache.sis.util.resources.ResourceCompilerMojo}) used
+ * for building Apache SIS. See the Maven-generated module description for 
more information.
+ *
+ * @author  Martin Desruisseaux (IRD, Geomatys)
+ * @since   0.3 (derived from geotk-2.2)
+ * @version 0.3
+ * @module
+ */
+package org.apache.sis.internal.maven;

Propchange: 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/maven/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sis/branches/JDK7/sis-build-helper/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-build-helper/src/site/apt/index.apt?rev=1427505&r1=1427504&r2=1427505&view=diff
==============================================================================
--- sis/branches/JDK7/sis-build-helper/src/site/apt/index.apt (original)
+++ sis/branches/JDK7/sis-build-helper/src/site/apt/index.apt Tue Jan  1 
17:59:06 2013
@@ -8,6 +8,14 @@ Building Apache SIS
   resource files or formatting the Javadoc. While any project could use it, 
this module is
   primarily for internal use by Apache SIS and may change in any future 
version.
 
+  Content:
+
+  * Javadoc taglets
+
+  * Localized resources compiler
+
+  * JAR files collector
+
 
 * Javadoc taglets
 
@@ -74,8 +82,45 @@ Building Apache SIS
   The resources compilation is part of the build process and does not need to 
be run explicitly.
   However, if necessary the resources compiler can be invoked alone by the 
following command line
   in the module containing the resources to recompile. This is sometime useful 
for regenerating
-  the constants in the <<<Keys>>> inner class.
+  the constants in the <<<Keys>>> inner class in a feaster way than building 
the project.
 
 +-----------------------------------------------------
 mvn org.apache.sis:sis-build-helper:compile-resources
 +-----------------------------------------------------
+
+
+* JAR files collector
+
+  Links or copies all JAR files (including dependencies) in the 
<<<target/binaries>>> directory of the parent project.
+  This plugin performs a work similar to the standard Maven assembly plugin 
work, with the following differences:
+
+  * In multi-modules projects, this plugin does not create anything in the 
<<<target>>> directory of sub-modules.
+    Instead, this plugin groups everything in the <<<target/binaries>>> 
directory of the parent module.
+
+  * This plugin does not create any ZIP file. It only links or copies JAR 
files.
+    The JDK7 and above versions of this plugin use hard links on platforms 
that support them,
+    so execution of this plugin should be very cheap and consume few disk 
space.
+
+  * Dependencies already present in the <<<target/binaries>>> directory are 
presumed stables and
+    are not overwritten. Only artefacts produced by the Maven build are 
unconditionally overwritten.
+
+  This plugin can be activated by the following fragment in the parent 
<<<pom.xml>>> file:
+
++-----------------------------------------------------
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.sis</groupId>
+        <artifactId>sis-build-helper</artifactId>
+        <version>0.3-jdk7-SNAPSHOT</version>
+        <executions>
+          <execution>
+            <goals>
+              <goal>collect-jars</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
++-----------------------------------------------------

Modified: sis/branches/JDK7/sis-utility/pom.xml
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/pom.xml?rev=1427505&r1=1427504&r2=1427505&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/pom.xml (original)
+++ sis/branches/JDK7/sis-utility/pom.xml Tue Jan  1 17:59:06 2013
@@ -94,18 +94,21 @@ Miscellaneous utilities.
 
 
   <!-- ===========================================================
-           Compile properties files into resources UTF files
+           Build configuration
        =========================================================== -->
   <build>
     <plugins>
+
+      <!-- Compile properties files into resources UTF files and collect JAR 
files. -->
       <plugin>
         <groupId>org.apache.sis</groupId>
         <artifactId>sis-build-helper</artifactId>
-        <version>${project.version}</version>
+        <version>${sis.plugin.version}</version>
         <executions>
           <execution>
             <goals>
               <goal>compile-resources</goal>
+              <goal>collect-jars</goal>
             </goals>
           </execution>
         </executions>

Modified: 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java?rev=1427505&r1=1427504&r2=1427505&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java 
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java 
Tue Jan  1 17:59:06 2013
@@ -143,11 +143,15 @@ public enum About {
      * version number):
      *
      * {@preformat java
-     *     java -jar sis-utility.jar
+     *     java -jar target/binaries/sis-utility.jar
      * }
      *
-     * By default this command prints all information except the {@link 
#LIBRARIES} section,
-     * which is verbose. Available options are:
+     * "{@code target/binaries}" is the default location where SIS JAR files 
are grouped together
+     * with their dependencies after a Maven build. This directory can be 
replaced by any path to
+     * a directory providing the required dependencies.
+     *
+     * <p>By default the above command prints all information except the 
{@link #LIBRARIES} section,
+     * because the later is considered too verbose. Available options are:</p>
      *
      * <ul>
      *   <li>{@code --version}: prints only Apache SIS version number.</li>


Reply via email to