View Javadoc

1   /**
2    * The MIT License
3    * 
4    * Copyright 2006-2012 The Codehaus.
5    * 
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   * 
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   * 
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.codehaus.mojo.appassembler.daemon.generic;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
30  import org.codehaus.mojo.appassembler.daemon.DaemonGenerator;
31  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
32  import org.codehaus.mojo.appassembler.daemon.merge.DaemonMerger;
33  import org.codehaus.mojo.appassembler.model.Classpath;
34  import org.codehaus.mojo.appassembler.model.Daemon;
35  import org.codehaus.mojo.appassembler.model.Dependency;
36  import org.codehaus.mojo.appassembler.model.io.stax.AppassemblerModelStaxWriter;
37  import org.codehaus.plexus.logging.AbstractLogEnabled;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.codehaus.plexus.util.IOUtil;
40  
41  import javax.xml.stream.XMLStreamException;
42  import java.io.File;
43  import java.io.FileOutputStream;
44  import java.io.IOException;
45  import java.io.OutputStreamWriter;
46  import java.util.Iterator;
47  
48  /**
49   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
50   * @version $Id: GenericDaemonGenerator.java 16492 2012-04-29 18:26:21Z khmarbaise $
51   * @plexus.component role-hint="generic"
52   */
53  public class GenericDaemonGenerator extends AbstractLogEnabled implements DaemonGenerator
54  {
55      /**
56       * @plexus.requirement
57       */
58      private DaemonMerger daemonMerger;
59  
60      // -----------------------------------------------------------------------
61      // DaemonGenerator Implementation
62      // -----------------------------------------------------------------------
63  
64      /*
65       * (non-Javadoc)
66       * @see org.codehaus.mojo.appassembler.daemon.DaemonGenerator#generate(org.codehaus.mojo.appassembler.daemon.
67       * DaemonGenerationRequest)
68       */
69      public void generate ( DaemonGenerationRequest request ) throws DaemonGeneratorException
70      {
71          // -----------------------------------------------------------------------
72          // Create the daemon from the Maven project
73          // -----------------------------------------------------------------------
74  
75          Daemon createdDaemon = createDaemon ( request.getMavenProject ( ), request.getRepositoryLayout ( ) );
76  
77          // -----------------------------------------------------------------------
78          // Merge the given stub daemon and the generated
79          // -----------------------------------------------------------------------
80  
81          Daemon mergedDaemon = daemonMerger.mergeDaemons ( request.getDaemon ( ), createdDaemon );
82  
83          // -----------------------------------------------------------------------
84          // Write out the project
85          // -----------------------------------------------------------------------
86  
87          OutputStreamWriter writer = null;
88  
89          try
90          {
91  
92              FileUtils.forceMkdir ( request.getOutputDirectory ( ) );
93  
94              File outputFile = new File ( request.getOutputDirectory ( ), mergedDaemon.getId ( ) + ".xml" );
95  
96              FileOutputStream fos = new FileOutputStream ( outputFile );
97  
98              writer = new OutputStreamWriter ( fos, "UTF-8" );
99  
100             AppassemblerModelStaxWriter staxWriter = new AppassemblerModelStaxWriter ( );
101             staxWriter.write ( writer, mergedDaemon );
102         }
103         catch ( IOException e )
104         {
105             throw new DaemonGeneratorException (
106                     "Error while writing output file: " + request.getOutputDirectory ( ), e );
107         }
108         catch ( XMLStreamException e )
109         {
110             throw new DaemonGeneratorException (
111                     "Error while writing output file: " + request.getOutputDirectory ( ), e );
112         }
113         finally
114         {
115             IOUtil.close ( writer );
116         }
117     }
118 
119     // -----------------------------------------------------------------------
120     // Private
121     // -----------------------------------------------------------------------
122 
123     private Daemon createDaemon ( MavenProject project, ArtifactRepositoryLayout layout )
124     {
125         Daemon complete = new Daemon ( );
126 
127         complete.setClasspath ( new Classpath ( ) );
128 
129         // -----------------------------------------------------------------------
130         // Add the project itself as a dependency.
131         // -----------------------------------------------------------------------
132         Dependency projectDependency = new Dependency ( );
133         Artifact projectArtifact = project.getArtifact ( );
134         projectArtifact.isSnapshot ( );
135         projectDependency.setGroupId ( projectArtifact.getGroupId ( ) );
136         projectDependency.setArtifactId ( projectArtifact.getArtifactId ( ) );
137         projectDependency.setVersion ( projectArtifact.getVersion ( ) );
138         projectDependency.setClassifier ( projectArtifact.getClassifier ( ) );
139         projectDependency.setRelativePath ( layout.pathOf ( projectArtifact ) );
140         complete.getClasspath ( ).addDependency ( projectDependency );
141 
142         // -----------------------------------------------------------------------
143         // Add all the dependencies of the project.
144         // -----------------------------------------------------------------------
145         for ( Iterator it = project.getRuntimeArtifacts ( ).iterator ( ); it.hasNext ( ); )
146         {
147             Artifact artifact = ( Artifact ) it.next ( );
148 
149             artifact.isSnapshot ( );
150 
151             Dependency dependency = new Dependency ( );
152             dependency.setGroupId ( artifact.getGroupId ( ) );
153             dependency.setArtifactId ( artifact.getArtifactId ( ) );
154             dependency.setVersion ( artifact.getVersion ( ) );
155 
156             dependency.setClassifier ( artifact.getClassifier ( ) );
157             dependency.setRelativePath ( layout.pathOf ( artifact ) );
158 
159             complete.getClasspath ( ).addDependency ( dependency );
160         }
161 
162         return complete;
163     }
164 }