View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse.writers.rad;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.eclipse.Constants;
27  import org.apache.maven.plugin.eclipse.Messages;
28  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
29  import org.apache.maven.plugin.ide.IdeDependency;
30  import org.apache.maven.plugin.ide.IdeUtils;
31  import org.apache.maven.plugin.ide.JeeUtils;
32  import org.apache.maven.plugin.logging.Log;
33  import org.codehaus.plexus.util.FileUtils;
34  
35  /**
36   * Copy all dependent jar in the directorys where RAD6 needs then to use the runtime enviorment in RAD6. so all
37   * dependent jars in the EAR rootdirectory and all dependend jars in the WAR WEB-INF/lib directory
38   * 
39   * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven</a>
40   */
41  public class RadLibCopier
42      extends AbstractEclipseWriter
43  {
44  
45      /**
46       * copy the jars in the apropreate directorys.
47       * 
48       * @throws MojoExecutionException when writing the config files was not possible
49       */
50      public void write()
51          throws MojoExecutionException
52      {
53          IdeDependency[] deps = config.getDeps();
54  
55          String packaging = config.getPackaging();
56          if ( Constants.PROJECT_PACKAGING_EAR.equals( packaging ) )
57          {
58              handleEarLibs( deps );
59          }
60          else if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
61          {
62              handleWarLibs( deps );
63          }
64      }
65  
66      /**
67       * Copies the Artifact after building the destination file name if overridden. This method also checks if the
68       * classifier is set and adds it to the destination file name if needed.
69       * 
70       * @param deps representing the dependencies to be copied.
71       * @param destDir where should the atifact go.
72       * @throws MojoExecutionException with a message if an error occurs.
73       * @see DependencyUtil#copyFile(File, File, Log)
74       * @see DependencyUtil#getFormattedFileName(Artifact, boolean)
75       */
76      private void copyArtifact( IdeDependency[] deps, File destDir )
77          throws MojoExecutionException
78      {
79          String[] oldFiles =
80              FileUtils.getFilesFromExtension( destDir.getAbsolutePath(),
81                                               new String[] { Constants.PROJECT_PACKAGING_JAR } );
82          for ( int index = 0; index < oldFiles.length; index++ )
83          {
84              if ( !new File( oldFiles[index] ).delete() )
85              {
86                  log.error( Messages.getString( "Rad6LibCopier.cantdeletefile", new Object[] { oldFiles[index] } ) );
87              }
88          }
89          for ( int index = 0; index < deps.length; index++ )
90          {
91              if ( !deps[index].isTestDependency() && !deps[index].isProvided() && !deps[index].isReferencedProject()
92                  && !deps[index].isSystemScoped() )
93              {
94                  copyFile( deps[index].getFile(), new File( destDir, deps[index].getFile().getName() ), log );
95              }
96          }
97      }
98  
99      /**
100      * Does the actual copy of the file and logging.
101      * 
102      * @param artifact represents the file to copy.
103      * @param destFile file name of destination file.
104      * @param log to use for output.
105      * @throws MojoExecutionException with a message if an error occurs.
106      */
107     private void copyFile( File artifact, File destFile, Log log )
108         throws MojoExecutionException
109     {
110         try
111         {
112             log.info( "Copying " + artifact.getAbsolutePath() + " to " + destFile );
113             FileUtils.copyFile( artifact, destFile );
114         }
115         catch ( IOException e )
116         {
117             throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e );
118         }
119     }
120 
121     /**
122      * EARs need the jars in the root directory.
123      * 
124      * @param deps dependencys to include
125      * @throws MojoExecutionException if the copying fails
126      */
127     private void handleEarLibs( IdeDependency[] deps )
128         throws MojoExecutionException
129     {
130         File targetDir = config.getProject().getBasedir();
131         copyArtifact( deps, targetDir );
132     }
133 
134     /**
135      * WARs need the jars in the WEB-INF/lib directory.
136      * 
137      * @param deps dependencys to include
138      * @throws MojoExecutionException if the copying fails
139      */
140     private void handleWarLibs( IdeDependency[] deps )
141         throws MojoExecutionException
142     {
143         File basedir = config.getProject().getBasedir();
144 
145         // Generating web content settings based on war plug-in warSourceDirectory property
146         File warSourceDirectory =
147             new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
148                                                  "warSourceDirectory", //$NON-NLS-1$
149                                                  "src/main/webapp" ) ); //$NON-NLS-1$
150         String webContentDir =
151             IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
152 
153         String srcMainWebappWebInfLibDirName =
154             basedir.getAbsolutePath() + File.separatorChar + webContentDir + File.separatorChar + "WEB-INF"
155                 + File.separatorChar + "lib";
156 
157         File srcMainWebappWebInfLibDir = new File( srcMainWebappWebInfLibDirName );
158         srcMainWebappWebInfLibDir.mkdirs();
159 
160         copyArtifact( deps, srcMainWebappWebInfLibDir );
161     }
162 
163 }