forked from geodmms/xdgnjobs

?? ?
2010-03-31 a911850eb2a5ec8b8ac24c57ce8dcfd2e6800801
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.ximple.eofms.maven;
 
import org.apache.maven.artifact.Artifact;
 
// Maven and Plexus dependencies
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
 
// J2SE dependencies
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
 
 
// Note: javadoc in class and fields descriptions must be XHTML.
/**
 * Copies <code>.jar</code> files in a single directory. Dependencies are copied as well,
 * except if already presents.
 *
 * @goal collect
 * @phase package
 * @source $URL$
 * @version $Id$
 * @author Martin Desruisseaux
 */
public class JarCollector extends AbstractMojo {
    /**
     * The sub directory to create inside the "target" directory.
     */
    private static final String SUB_DIRECTORY = "binaries";
 
    /**
     * The directory where JARs are to be copied. It should
     * be the "target" directory of the parent {@code pom.xml}.
     */
    private String collectDirectory;
 
    /**
     * Directory containing the generated JAR.
     *
     * @parameter expression="${project.build.directory}"
     * @required
     */
    private String outputDirectory;
 
    /**
     * Name of the generated JAR.
     *
     * @parameter expression="${project.build.finalName}"
     * @required
     */
    private String jarName;
 
    /**
     * Project dependencies.
     *
     * @parameter expression="${project.artifacts}"
     * @required
     */
    private Set /*<Artifact>*/ dependencies;
 
    /**
     * The Maven project running this plugin.
     *
     * @parameter expression="${project}"
     * @required
     */
    private MavenProject project;
 
    /**
     * Copies the {@code .jar} files to the collect directory.
     *
     * @throws MojoExecutionException if the plugin execution failed.
     */
    public void execute() throws MojoExecutionException {
        /*
         * Gets the parent "target" directory.
         */
        MavenProject parent = project;
 
        while (parent.hasParent()) {
            parent = parent.getParent();
        }
 
        collectDirectory = parent.getBuild().getDirectory();
 
        /*
         * Now collects the JARs.
         */
        try {
            collect();
        } catch (IOException e) {
            throw new MojoExecutionException("Error collecting the JAR file.", 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 = new File(outputDirectory, jarName + ".jar");
 
        if (!jarFile.isFile()) {
            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.
         */
        collect = new File(collect, SUB_DIRECTORY);
 
        if (!collect.exists()) {
            if (!collect.mkdir()) {
                throw new MojoExecutionException("Failed to create binaries directory.");
            }
        }
 
        int count = 1;
        FileUtils.copyFileToDirectory(jarFile, collect);
 
        if (dependencies != null) {
            for (final Iterator it = dependencies.iterator(); it.hasNext();) {
                final Artifact artifact = (Artifact) it.next();
                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();
                    final File copy = new File(collect, file.getName());
 
                    if (!copy.exists()) {
                        /*
                         * Copies the dependency only if it was not already copied. Note that
                         * the module's JAR was copied inconditionnaly above (because it may
                         * be the result of a new compilation). If a Geotools JAR from the
                         * dependencies list changed, it will be copied inconditionnaly when
                         * the module for this JAR will be processed by Maven.
                         */
                        FileUtils.copyFileToDirectory(file, collect);
                        count++;
                    }
                }
            }
        }
 
        getLog().info("Copied " + count + " JAR to parent directory.");
    }
}