1 package org.apache.maven.plugins.dependency.utils.markers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.plugin.MojoExecutionException;
27
28
29
30
31 public class DefaultFileMarkerHandler
32 implements MarkerHandler
33 {
34
35
36
37 protected Artifact artifact;
38
39
40
41
42 protected File markerFilesDirectory;
43
44
45
46
47 public DefaultFileMarkerHandler( File theMarkerFilesDirectory )
48 {
49 this.markerFilesDirectory = theMarkerFilesDirectory;
50 }
51
52
53
54
55
56 public DefaultFileMarkerHandler( Artifact theArtifact, File theMarkerFilesDirectory )
57 {
58 this.artifact = theArtifact;
59 this.markerFilesDirectory = theMarkerFilesDirectory;
60 }
61
62
63
64
65
66
67 protected File getMarkerFile()
68 {
69 return new File( this.markerFilesDirectory, this.artifact.getId().replace( ':', '-' ) + ".marker" );
70 }
71
72
73
74
75
76
77
78
79
80
81 @Override
82 public boolean isMarkerSet()
83 throws MojoExecutionException
84 {
85 File marker = getMarkerFile();
86 return marker.exists();
87 }
88
89 @Override
90 public boolean isMarkerOlder( Artifact artifact1 )
91 throws MojoExecutionException
92 {
93 File marker = getMarkerFile();
94 if ( marker.exists() )
95 {
96 return artifact1.getFile().lastModified() > marker.lastModified();
97 }
98 else
99 {
100
101
102 return true;
103 }
104 }
105
106 @Override
107 public void setMarker()
108 throws MojoExecutionException
109 {
110 File marker = getMarkerFile();
111
112 try
113 {
114 marker.getParentFile().mkdirs();
115 }
116 catch ( NullPointerException e )
117 {
118
119 }
120 try
121 {
122 marker.createNewFile();
123 }
124 catch ( IOException e )
125 {
126 throw new MojoExecutionException( "Unable to create Marker: " + marker.getAbsolutePath(), e );
127 }
128
129
130 try
131 {
132 long ts;
133 if ( this.artifact != null && this.artifact.getFile() != null )
134 {
135 ts = this.artifact.getFile().lastModified();
136 }
137 else
138 {
139 ts = System.currentTimeMillis();
140 }
141 if ( !marker.setLastModified( ts ) )
142 {
143 throw new MojoExecutionException( "Unable to update last modified timestamp on marker file "
144 + marker.getAbsolutePath() );
145
146 }
147 }
148 catch ( Exception e )
149 {
150 throw new MojoExecutionException( "Unable to update Marker timestamp: " + marker.getAbsolutePath(), e );
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163 @Override
164 public boolean clearMarker()
165 throws MojoExecutionException
166 {
167 File marker = getMarkerFile();
168 return marker.delete();
169 }
170
171
172
173
174 public Artifact getArtifact()
175 {
176 return this.artifact;
177 }
178
179
180
181
182 @Override
183 public void setArtifact( Artifact artifact )
184 {
185 this.artifact = artifact;
186 }
187
188
189
190
191 public File getMarkerFilesDirectory()
192 {
193 return this.markerFilesDirectory;
194 }
195
196
197
198
199 public void setMarkerFilesDirectory( File markerFilesDirectory )
200 {
201 this.markerFilesDirectory = markerFilesDirectory;
202 }
203 }