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