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