1 package org.apache.maven.jelly.tags.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 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
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
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
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 }