Skip to content

Commit 59260b6

Browse files
committed
[buildtask] implemented as mojo
1 parent ce9f400 commit 59260b6

File tree

3 files changed

+103
-71
lines changed

3 files changed

+103
-71
lines changed

pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,18 @@
245245
<type>test-jar</type>
246246
<scope>test</scope>
247247
</dependency>
248+
249+
<dependency>
250+
<groupId>org.apache.maven.plugin-tools</groupId>
251+
<artifactId>maven-plugin-annotations</artifactId>
252+
<version>3.6.4</version>
253+
<scope>test</scope>
254+
</dependency>
255+
<dependency>
256+
<groupId>org.apache.maven</groupId>
257+
<artifactId>maven-plugin-api</artifactId>
258+
<version>3.8.3</version>
259+
<scope>test</scope>
260+
</dependency>
248261
</dependencies>
249262
</project>

src/test/java/msBuildTask/CreateIso.java

+53-41
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,40 @@
2222

2323
package msBuildTask;
2424

25-
import java.util.concurrent.Callable;
26-
import java.util.logging.Level;
27-
import java.util.logging.Logger;
25+
import java.nio.file.Paths;
2826

2927
import discUtils.iso9660.BootDeviceEmulation;
3028
import discUtils.iso9660.CDBuilder;
3129
import dotnet4j.io.FileAccess;
3230
import dotnet4j.io.FileMode;
3331
import dotnet4j.io.FileStream;
3432
import dotnet4j.io.Stream;
33+
import org.apache.maven.plugin.MojoExecutionException;
34+
import org.apache.maven.plugin.MojoFailureException;
35+
import org.apache.maven.plugin.logging.Log;
36+
import org.apache.maven.plugins.annotations.LifecyclePhase;
37+
import org.apache.maven.plugins.annotations.Mojo;
3538

3639

37-
public class CreateIso implements Callable {
38-
static final Logger logger = Logger.getLogger(CreateIso.class.getName());
40+
@Mojo(name = "discutil-plugin-create-iso", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
41+
public class CreateIso implements org.apache.maven.plugin.Mojo {
42+
43+
private Log log;
3944

4045
public CreateIso() {
41-
setUseJoliet(true);
46+
useJoliet = true;
4247
}
4348

4449
/**
4550
* The name of the ISO file to create.
4651
*/
47-
private ITaskItem fileName;
52+
private String fileName;
4853

49-
public ITaskItem getFileName() {
54+
public String getFileName() {
5055
return fileName;
5156
}
5257

53-
public void setFileName(ITaskItem value) {
58+
public void setFileName(String value) {
5459
fileName = value;
5560
}
5661

@@ -83,26 +88,26 @@ public void setVolumeLabel(String value) {
8388
/**
8489
* The files to add to the ISO.
8590
*/
86-
private ITaskItem[] sourceFiles;
91+
private String[] sourceFiles;
8792

88-
public ITaskItem[] getSourceFiles() {
93+
public String[] getSourceFiles() {
8994
return sourceFiles;
9095
}
9196

92-
public void setSourceFiles(ITaskItem[] value) {
97+
public void setSourceFiles(String[] value) {
9398
sourceFiles = value;
9499
}
95100

96101
/**
97102
* The boot image to add to the ISO.
98103
*/
99-
private ITaskItem bootImage;
104+
private String bootImage;
100105

101-
public ITaskItem getBootImage() {
106+
public String getBootImage() {
102107
return bootImage;
103108
}
104109

105-
public void setBootImage(ITaskItem value) {
110+
public void setBootImage(String value) {
106111
bootImage = value;
107112
}
108113

@@ -127,18 +132,34 @@ public void setUpdateIsolinuxBootTable(boolean value) {
127132
* If the source file is C:\MyDir\MySubDir\file.txt, and RemoveRoot is C:\MyDir, the ISO will
128133
* contain \MySubDir\file.txt. If not specified, the file would be named \MyDir\MySubDir\file.txt.
129134
*/
130-
private ITaskItem[] removeRoots = new ITaskItem[1];
135+
private String[] removeRoots = new String[1];
131136

132-
public ITaskItem[] getRemoveRoots() {
137+
public String[] getRemoveRoots() {
133138
return removeRoots;
134139
}
135140

136-
public void setRemoveRoots(ITaskItem[] value) {
141+
public void setRemoveRoots(String[] value) {
137142
removeRoots = value;
138143
}
139144

140-
public Boolean call() {
141-
logger.info("Creating ISO file: '%s'", getFileName().ItemSpec);
145+
private String getDestinationPath(String sourceFile) {
146+
String fullPath = Paths.get(sourceFile).toAbsolutePath().toString();
147+
if (getRemoveRoots() != null) {
148+
for (String root : getRemoveRoots()) {
149+
String rootPath = Paths.get(root).toAbsolutePath().toString();
150+
if (fullPath.startsWith(rootPath)) {
151+
return fullPath.substring(rootPath.length());
152+
}
153+
}
154+
}
155+
156+
// Not under a known root - so full path (minus drive)...
157+
return sourceFile;
158+
}
159+
160+
@Override
161+
public void execute() throws MojoExecutionException, MojoFailureException {
162+
log.info(String.format("Creating ISO file: '%s'", fileName));
142163
try {
143164
CDBuilder builder = new CDBuilder();
144165
builder.setUseJoliet(getUseJoliet());
@@ -148,42 +169,33 @@ public Boolean call() {
148169

149170
Stream bootImageStream = null;
150171
if (getBootImage() != null) {
151-
bootImageStream = new FileStream(getBootImage().GetMetadata("FullPath"), FileMode.Open, FileAccess.Read);
172+
bootImageStream = new FileStream(Paths.get(bootImage).toAbsolutePath().toString(), FileMode.Open, FileAccess.Read);
152173
builder.setBootImage(bootImageStream, BootDeviceEmulation.NoEmulation, 0);
153174
builder.setUpdateIsolinuxBootTable(getUpdateIsolinuxBootTable());
154175
}
155176

156-
for (ITaskItem sourceFile : getSourceFiles()) {
157-
builder.addFile(GetDestinationPath(sourceFile), sourceFile.getMetadata("FullPath"));
177+
for (String sourceFile : getSourceFiles()) {
178+
builder.addFile(getDestinationPath(sourceFile), Paths.get(sourceFile).toAbsolutePath().toString());
158179
}
159180
try {
160-
builder.Build(getFileName().ItemSpec);
181+
builder.build(fileName);
161182
} finally {
162183
if (bootImageStream != null) {
163184
bootImageStream.close();
164185
}
165-
166186
}
167187
} catch (Exception e) {
168-
logger.log(Level.SEVERE, e.getMessage(), e);
169-
return false;
188+
log.error(e.getMessage(), e);
170189
}
171-
172-
return !logger.HasLoggedErrors;
173190
}
174191

175-
private String getDestinationPath(ITaskItem sourceFile) {
176-
String fullPath = sourceFile.GetMetadata("FullPath");
177-
if (getRemoveRoots() != null) {
178-
for (Callable root : getRemoveRoots()) {
179-
String rootPath = root.getMetadata("FullPath");
180-
if (fullPath.startsWith(rootPath)) {
181-
return fullPath.substring(rootPath.length());
182-
}
183-
}
184-
}
192+
@Override
193+
public void setLog(Log log) {
194+
this.log = log;
195+
}
185196

186-
// Not under a known root - so full path (minus drive)...
187-
return sourceFile.getMetadata("Directory") + sourceFile.getMetadata("FileName") + sourceFile.getMetadata("Extension");
197+
@Override
198+
public Log getLog() {
199+
return log;
188200
}
189201
}

src/test/java/msBuildTask/CreateSquashFileSystem.java

+37-30
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,44 @@
2222

2323
package msBuildTask;
2424

25-
import java.util.concurrent.Callable;
26-
import java.util.logging.Level;
27-
import java.util.logging.Logger;
25+
import java.nio.file.Paths;
2826

2927
import discUtils.squashFs.SquashFileSystemBuilder;
28+
import org.apache.maven.plugin.MojoExecutionException;
29+
import org.apache.maven.plugin.MojoFailureException;
30+
import org.apache.maven.plugin.logging.Log;
31+
import org.apache.maven.plugins.annotations.LifecyclePhase;
32+
import org.apache.maven.plugins.annotations.Mojo;
3033

3134

32-
public class CreateSquashFileSystem implements Callable<Boolean> {
33-
static final Logger logger = Logger.getLogger(CreateSquashFileSystem.class.getName());
35+
@Mojo(name = "discutil-plugin-create-squashfs", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
36+
public class CreateSquashFileSystem implements org.apache.maven.plugin.Mojo {
3437

35-
public CreateSquashFileSystem() {
36-
}
38+
private Log log;
3739

3840
/**
3941
* The name of the file to create, containing the filesystem image.
4042
*/
41-
private ITaskItem fileName;
43+
private String fileName;
4244

43-
public ITaskItem getFileName() {
45+
public String getFileName() {
4446
return fileName;
4547
}
4648

47-
public void setFileName(ITaskItem value) {
49+
public void setFileName(String value) {
4850
fileName = value;
4951
}
5052

5153
/**
5254
* The files to add to the filesystem image.
5355
*/
54-
private ITaskItem[] sourceFiles;
56+
private String[] sourceFiles;
5557

56-
public ITaskItem[] getSourceFiles() {
58+
public String[] getSourceFiles() {
5759
return sourceFiles;
5860
}
5961

60-
public void setSourceFiles(ITaskItem[] value) {
62+
public void setSourceFiles(String[] value) {
6163
sourceFiles = value;
6264
}
6365

@@ -68,37 +70,42 @@ public void setSourceFiles(ITaskItem[] value) {
6870
* C:\MyDir, the filesystem will contain \MySubDir\file.txt. If not
6971
* specified, the file would be named \MyDir\MySubDir\file.txt.
7072
*/
71-
private ITaskItem removeRoot;
73+
private String removeRoot;
7274

73-
public ITaskItem getRemoveRoot() {
75+
public String getRemoveRoot() {
7476
return removeRoot;
7577
}
7678

77-
public void setRemoveRoot(ITaskItem value) {
79+
public void setRemoveRoot(String value) {
7880
removeRoot = value;
7981
}
8082

81-
public Boolean call() {
82-
logger.log(String.format("Creating SquashFS file: '%s'", getFileName().ItemSpec));
83+
@Override
84+
public void execute() throws MojoExecutionException, MojoFailureException {
85+
log.info(String.format("Creating SquashFS file: '%s'", fileName));
8386
try {
8487
SquashFileSystemBuilder builder = new SquashFileSystemBuilder();
85-
for (ITaskItem sourceFile : getSourceFiles()) {
86-
if (this.getRemoveRoot() != null) {
87-
String location = (sourceFile.GetMetadata("FullPath")).Replace(this.getRemoveRoot().GetMetadata("FullPath"),
88-
"");
89-
builder.addFile(location, sourceFile.GetMetadata("FullPath"));
88+
for (String sourceFile : sourceFiles) {
89+
if (this.removeRoot != null) {
90+
String location = Paths.get(sourceFile).toAbsolutePath().toString().replace(Paths.get(this.removeRoot).toAbsolutePath().toString(), "");
91+
builder.addFile(location, Paths.get(sourceFile).toAbsolutePath().toString());
9092
} else {
91-
builder.addFile(sourceFile.GetMetadata("Directory") + sourceFile.GetMetadata("FileName") +
92-
sourceFile.GetMetadata("Extension"),
93-
sourceFile.GetMetadata("FullPath"));
93+
builder.addFile(sourceFile, Paths.get(sourceFile).toAbsolutePath().toString());
9494
}
9595
}
96-
builder.Build(getFileName().ItemSpec);
96+
builder.build(fileName);
9797
} catch (Exception e) {
98-
logger.log(Level.SEVERE, e.getMessage(), e);
99-
return false;
98+
log.error(e.getMessage(), e);
10099
}
100+
}
101+
102+
@Override
103+
public void setLog(Log log) {
104+
this.log = log;
105+
}
101106

102-
return !logger.HasLoggedErrors;
107+
@Override
108+
public Log getLog() {
109+
return log;
103110
}
104111
}

0 commit comments

Comments
 (0)