1 package org.apache.maven.jelly.tags.maven;
2
3 /* ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 */
20
21 import java.io.File;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.commons.jelly.JellyTagException;
26 import org.apache.commons.jelly.MissingAttributeException;
27 import org.apache.commons.jelly.XMLOutput;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.maven.jelly.tags.BaseTagSupport;
31 import org.apache.maven.project.Resource;
32 import org.apache.tools.ant.types.FileSet;
33 import org.apache.tools.ant.types.PatternSet;
34
35 /**
36 * A tag to copy resources to a given directory
37 *
38 * @version $Revision: 517014 $
39 */
40 public class CopyResources
41 extends BaseTagSupport
42 {
43 /** LOGGER for debug output */
44 private static final Log LOGGER = LogFactory.getLog( CopyResources.class );
45
46 /** the resources to copy */
47 private List resources;
48
49 /** the directory to copy to */
50 private String todir;
51
52 /**
53 * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
54 */
55 public void doTag( XMLOutput output )
56 throws MissingAttributeException, JellyTagException
57 {
58 if ( ( resources == null ) || ( todir == null ) )
59 {
60 LOGGER.warn( "resources or todir was null" );
61 return;
62 }
63 // <j:forEach var="resource" items="${resources}">
64 // <j:set var="resourceDirectoryPresent" value="false"/>
65 //
66 // <util:available file="${resource.directory}">
67 // <j:set var="resourceDirectoryPresent" value="true"/>
68 // </util:available>
69 //
70 // <j:if test="${resourceDirectoryPresent == 'true'}">
71 // <j:choose>
72 // <j:when test="${resource.targetPath ==''}">
73 // <j:set var="targetDirectory" value="${todir}"/>
74 // </j:when>
75 // <j:otherwise>
76 // <j:set var="targetDirectory" value="${todir}/${resource.targetPath}"/>
77 // </j:otherwise>
78 // </j:choose>
79 // <copy todir="${targetDirectory}" filtering="${resource.filtering}"
80 // overwrite="${resource.filtering}">
81 // <fileset dir="${resource.directory}">
82 // <j:if test="${resources.includes.isEmpty()}">
83 // <include name="**/**"/>
84 // </j:if>
85 // <j:forEach var="include" items="${resource.includes}">
86 // <include name="${include}"/>
87 // </j:forEach>
88 // <j:forEach var="exclude" items="${resource.excludes}">
89 // <exclude name="${exclude}"/>
90 // </j:forEach>
91 // </fileset>
92 // </copy>
93 // </j:if>
94 // </j:forEach>
95
96 for ( Iterator iter = resources.iterator(); iter.hasNext(); )
97 {
98 Resource resource = (Resource) iter.next();
99 String directoryName = resource.getDirectory();
100 if ( directoryName == null )
101 {
102 continue;
103 }
104 File directory = new File( directoryName );
105 if ( directory.exists() && directory.isDirectory() && directory.canRead() )
106 {
107 StringBuffer targetDirectoryBuffer = new StringBuffer( todir );
108 String targetPath = resource.getTargetPath();
109 if ( ( targetPath != null ) && !"".equals( targetPath.trim() ) )
110 {
111 targetDirectoryBuffer.append( '/' ).append( targetPath );
112 }
113 File targetDirectory = new File( targetDirectoryBuffer.toString() );
114 // copy to targetDirectory....
115 org.apache.tools.ant.taskdefs.Copy copyTask = new org.apache.tools.ant.taskdefs.Copy();
116 copyTask.setProject( getMavenContext().getAntProject() );
117 copyTask.setTodir( targetDirectory );
118 copyTask.setPreserveLastModified( true );
119 copyTask.setFiltering( resource.isFiltering() );
120 copyTask.setOverwrite( resource.isFiltering() );
121 FileSet fileSet = new FileSet();
122 fileSet.setDir( directory );
123 if ( resource.getIncludes().isEmpty() )
124 {
125 PatternSet.NameEntry entry = fileSet.createInclude();
126 entry.setName( "**/**" );
127 }
128 // add includes
129 for ( Iterator incIter = resource.getIncludes().iterator(); incIter.hasNext(); )
130 {
131 String include = (String) incIter.next();
132 PatternSet.NameEntry entry = fileSet.createInclude();
133 entry.setName( include );
134 }
135 // add excludes
136 for ( Iterator incIter = resource.getExcludes().iterator(); incIter.hasNext(); )
137 {
138 String exclude = (String) incIter.next();
139 PatternSet.NameEntry entry = fileSet.createExclude();
140 entry.setName( exclude );
141 }
142 copyTask.addFileset( fileSet );
143 copyTask.execute();
144 }
145 else
146 {
147 LOGGER.debug( "todir is either not a directory, not writable or not there" );
148 }
149 }
150
151 }
152
153 /**
154 * @param list the resources to copy
155 */
156 public void setResources( List list )
157 {
158 resources = list;
159 }
160
161 /**
162 * @param string the directory to copy them to
163 */
164 public void setTodir( String string )
165 {
166 todir = string;
167 }
168
169 }