Skip to content

Commit 3eaf5fb

Browse files
committed
feat: Add skips and renamed goal
BREAKING CHANGE: Goal has been renamed from attach-reqstool-zip to assemble-and-attach-zip-artifact
1 parent fd2a389 commit 3eaf5fb

3 files changed

Lines changed: 106 additions & 85 deletions

File tree

‎pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>se.lfv.reqstool</groupId>
99
<artifactId>reqstool-maven-plugin</artifactId>
1010
<packaging>maven-plugin</packaging>
11-
<version>0.0.4</version>
11+
<version>0.1.0</version>
1212

1313
<name>${project.artifactId}</name>
1414
<description>Reqstool - Maven Plugin</description>
@@ -262,4 +262,4 @@
262262
</plugins>
263263
</pluginManagement>
264264
</build>
265-
</project>
265+
</project>

‎src/main/java/se/lfv/reqstool/plugins/maven/RequirementsToolMojo.java‎

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,48 @@
1111
import java.util.Locale;
1212
import java.util.zip.ZipEntry;
1313
import java.util.zip.ZipOutputStream;
14+
15+
import javax.inject.Inject;
16+
1417
import org.apache.commons.io.FileUtils;
1518
import org.apache.maven.plugin.AbstractMojo;
1619
import org.apache.maven.plugin.MojoExecutionException;
1720
import org.apache.maven.plugin.logging.Log;
18-
import org.apache.maven.plugins.annotations.Component;
1921
import org.apache.maven.plugins.annotations.LifecyclePhase;
2022
import org.apache.maven.plugins.annotations.Mojo;
2123
import org.apache.maven.plugins.annotations.Parameter;
2224
import org.apache.maven.project.MavenProject;
2325
import org.apache.maven.project.MavenProjectHelper;
26+
2427
import com.fasterxml.jackson.databind.JsonNode;
2528
import com.fasterxml.jackson.databind.ObjectMapper;
2629
import com.fasterxml.jackson.databind.SerializationFeature;
2730
import com.fasterxml.jackson.databind.node.ObjectNode;
2831
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2932
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
3033

31-
@Mojo(name = "attach-reqstool-zip", defaultPhase = LifecyclePhase.PACKAGE)
34+
@Mojo(name = "assemble-and-attach-zip-artifact", defaultPhase = LifecyclePhase.PACKAGE)
3235
public class RequirementsToolMojo extends AbstractMojo {
3336

34-
public static final String TEST_RESULTS_SUREFIRE = "test_results/surefire";
37+
// Constants
3538

36-
public static final String MANUAL_VERIFICATION_RESULTS_YML = "manual_verification_results.yml";
39+
public static final String INPUT_DIR_TEST_RESULTS_FAILSAFE = "test_results/failsafe";
3740

38-
public static final String SOFTWARE_VERIFICATION_CASES_YML = "software_verification_cases.yml";
41+
public static final String INPUT_DIR_TEST_RESULTS_SUREFIRE = "test_results/surefire";
3942

40-
public static final String REQUIREMENTS_YML = "requirements.yml";
43+
public static final String INPUT_FILE_MANUAL_VERIFICATION_RESULTS_YML = "manual_verification_results.yml";
4144

42-
public static final String ANNOTATIONS_YML = "annotations.yml";
45+
public static final String INPUT_FILE_REQUIREMENTS_YML = "requirements.yml";
4346

44-
public static final String TESTS = "tests";
47+
public static final String INPUT_FILESOFTWARE_VERIFICATION_CASES_YML = "software_verification_cases.yml";
4548

46-
public static final String IMPLEMENTATIONS = "implementations";
49+
public static final String OUTPUT_FILE_ANNOTATIONS_YML_FILE = "annotations.yml";
4750

48-
public static final String REQUIREMENT_ANNOTATIONS = "requirement_annotations";
51+
public static final String XML_IMPLEMENTATIONS = "implementations";
4952

50-
public static final String TEST_RESULTS_FAILSAFE = "test_results/failsafe";
53+
public static final String XML_REQUIREMENT_ANNOTATIONS = "requirement_annotations";
5154

52-
public static final String REQSTOOL_DIRECTORY = "";
55+
public static final String XML_TESTS = "tests";
5356

5457
protected static final String YAML_LANG_SERVER_SCHEMA_INFO = "# yaml-language-server: $schema=https://raw.githubusercontent.com/Luftfartsverket/reqstool-client/main/src/reqstool/resources/schemas/v1/annotations.schema.json";
5558

@@ -80,34 +83,48 @@ public class RequirementsToolMojo extends AbstractMojo {
8083
@Parameter(property = "surefireReportsDir", defaultValue = "${project.build.directory}/surefire-reports")
8184
private File surefireReportsDir;
8285

86+
@Parameter(property = "zipArtifactFilename", defaultValue = "${project.build.finalName}-reqstool.zip")
87+
private String zipArtifactFilename;
88+
8389
@Parameter(defaultValue = "${project}", required = true, readonly = true)
84-
MavenProject project;
90+
private MavenProject project;
8591

86-
@Component
92+
@Inject
8793
private MavenProjectHelper projectHelper;
8894

89-
private String zipFilename;
90-
9195
@Parameter(defaultValue = "${log}", readonly = true)
9296
private Log log;
9397

94-
public void execute() throws MojoExecutionException {
95-
getLog().debug("Creating Reqstool Maven zip artifact");
98+
@Parameter(property = "reqstool.skip", defaultValue = "false")
99+
private boolean skip;
100+
101+
@Parameter(property = "reqstool.skipAssembleZipArtifact", defaultValue = "false")
102+
private boolean skipAssembleZipArtifact;
96103

97-
zipFilename = project.getBuild().getFinalName() + "-reqstool" + ".zip";
104+
@Parameter(property = "reqstool.skipAttachZipArtifact", defaultValue = "false")
105+
private boolean skipAttachZipArtifact;
98106

99-
JsonNode implementationsNode = yamlMapper.createObjectNode();
100-
JsonNode testsNode = yamlMapper.createObjectNode();
107+
public void execute() throws MojoExecutionException {
108+
if (skip) {
109+
getLog().info("Skipping execution of reqstool plugin");
110+
return;
111+
}
112+
113+
getLog().debug("Assembling and Attaching Reqstool Maven Zip Artifact");
101114

102115
try {
116+
117+
JsonNode implementationsNode = yamlMapper.createObjectNode();
118+
JsonNode testsNode = yamlMapper.createObjectNode();
119+
103120
if (requirementsAnnotationsFile.exists()) {
104121
implementationsNode = yamlMapper.readTree(requirementsAnnotationsFile)
105-
.path(REQUIREMENT_ANNOTATIONS)
106-
.path(IMPLEMENTATIONS);
122+
.path(XML_REQUIREMENT_ANNOTATIONS)
123+
.path(XML_IMPLEMENTATIONS);
107124
}
108125

109126
if (svcsAnnotationsFile.exists()) {
110-
testsNode = yamlMapper.readTree(svcsAnnotationsFile).path(REQUIREMENT_ANNOTATIONS).path(TESTS);
127+
testsNode = yamlMapper.readTree(svcsAnnotationsFile).path(XML_REQUIREMENT_ANNOTATIONS).path(XML_TESTS);
111128
}
112129

113130
JsonNode combinedOutputNode = combineOutput(implementationsNode, testsNode);
@@ -116,10 +133,21 @@ public void execute() throws MojoExecutionException {
116133
outputDirectory.mkdirs();
117134
}
118135

119-
writeCombinedOutputToFile(new File(outputDirectory, ANNOTATIONS_YML), combinedOutputNode);
136+
writeCombinedOutputToFile(new File(outputDirectory, OUTPUT_FILE_ANNOTATIONS_YML_FILE), combinedOutputNode);
120137

121-
createZipFile();
122-
attachArtifact();
138+
if (!skipAssembleZipArtifact) {
139+
assembleZipArtifact();
140+
}
141+
else {
142+
getLog().info("Skipping zip artifact assembly");
143+
}
144+
145+
if (!skipAttachZipArtifact) {
146+
attachArtifact();
147+
}
148+
else {
149+
getLog().info("Skipping zip artifact attachment");
150+
}
123151

124152
}
125153
catch (IOException e) {
@@ -128,69 +156,68 @@ public void execute() throws MojoExecutionException {
128156
}
129157

130158
static JsonNode combineOutput(JsonNode implementationsNode, JsonNode testsNode) {
131-
132-
// Create an ObjectNode for "requirement_annotations"
133159
ObjectNode requirementAnnotationsNode = yamlMapper.createObjectNode();
134160
if (!implementationsNode.isEmpty()) {
135-
requirementAnnotationsNode.set(IMPLEMENTATIONS, implementationsNode);
161+
requirementAnnotationsNode.set(XML_IMPLEMENTATIONS, implementationsNode);
136162
}
137163
if (!testsNode.isEmpty()) {
138-
requirementAnnotationsNode.set(TESTS, testsNode);
164+
requirementAnnotationsNode.set(XML_TESTS, testsNode);
139165
}
140166

141167
ObjectNode newNode = yamlMapper.createObjectNode();
142-
newNode.set(REQUIREMENT_ANNOTATIONS, requirementAnnotationsNode);
168+
newNode.set(XML_REQUIREMENT_ANNOTATIONS, requirementAnnotationsNode);
143169

144170
return newNode;
145171
}
146172

147-
static void writeCombinedOutputToFile(File outputFile, JsonNode combinedOutputNode) throws IOException {
173+
private void writeCombinedOutputToFile(File outputFile, JsonNode combinedOutputNode) throws IOException {
174+
getLog().info("Combining " + requirementsAnnotationsFile + " and " + svcsAnnotationsFile + " into "
175+
+ outputFile.getAbsolutePath());
148176

149177
try (Writer writer = new PrintWriter(
150178
new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8))) {
151-
152-
// Write the YAML language server schema info as a comment
153179
writer.write(YAML_LANG_SERVER_SCHEMA_INFO + System.lineSeparator());
154-
155-
// Write the JSON node data
156180
yamlMapper.writeValue(writer, combinedOutputNode);
157181
}
158182
}
159183

160-
private void createZipFile() throws IOException {
161-
File zipFile = new File(outputDirectory, zipFilename);
162-
getLog().info("Creating zip file: " + zipFile.getAbsolutePath());
184+
private void assembleZipArtifact() throws IOException {
185+
File zipFile = new File(outputDirectory, zipArtifactFilename);
186+
getLog().info("Assembling zip file: " + zipFile.getAbsolutePath());
163187

164188
try (FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zipOut = new ZipOutputStream(fos)) {
165189

166-
addFileToZip(zipOut, new File(datasetPath, REQUIREMENTS_YML), null);
167-
addFileToZip(zipOut, new File(datasetPath, SOFTWARE_VERIFICATION_CASES_YML), null);
168-
addFileToZip(zipOut, new File(datasetPath, MANUAL_VERIFICATION_RESULTS_YML), null);
169-
addFileToZip(zipOut, new File(outputDirectory, ANNOTATIONS_YML), null);
190+
addFileToZipArtifact(zipOut, new File(datasetPath, INPUT_FILE_REQUIREMENTS_YML), null);
191+
addFileToZipArtifact(zipOut, new File(datasetPath, INPUT_FILESOFTWARE_VERIFICATION_CASES_YML), null);
192+
addFileToZipArtifact(zipOut, new File(datasetPath, INPUT_FILE_MANUAL_VERIFICATION_RESULTS_YML), null);
193+
addFileToZipArtifact(zipOut, new File(outputDirectory, OUTPUT_FILE_ANNOTATIONS_YML_FILE), null);
170194

171-
addXmlFilesToZip(zipOut, failsafeReportsDir, new File(TEST_RESULTS_FAILSAFE));
172-
addXmlFilesToZip(zipOut, surefireReportsDir, new File(TEST_RESULTS_SUREFIRE));
195+
addXmlFilesToZipArtifact(zipOut, failsafeReportsDir, new File(INPUT_DIR_TEST_RESULTS_FAILSAFE));
196+
addXmlFilesToZipArtifact(zipOut, surefireReportsDir, new File(INPUT_DIR_TEST_RESULTS_SUREFIRE));
173197
}
198+
199+
getLog().info("Assembled zip artifact: " + zipFile.getAbsolutePath());
200+
174201
}
175202

176-
private void addXmlFilesToZip(ZipOutputStream zipOut, File directory, File targetDirectory) throws IOException {
203+
private void addXmlFilesToZipArtifact(ZipOutputStream zipOut, File directory, File targetDirectory)
204+
throws IOException {
177205
File[] files = directory.listFiles();
178206
if (files != null) {
179207
for (File file : files) {
180208
if (file.isDirectory()) {
181-
addXmlFilesToZip(zipOut, file, new File(targetDirectory, file.toString()));
209+
addXmlFilesToZipArtifact(zipOut, file, new File(targetDirectory, file.toString()));
182210
}
183211
else if (file.isFile() && file.getName().toLowerCase(Locale.US).endsWith(".xml")) {
184-
addFileToZip(zipOut, file, targetDirectory);
212+
addFileToZipArtifact(zipOut, file, targetDirectory);
185213
}
186214
}
187215
}
188216
}
189217

190-
private void addFileToZip(ZipOutputStream zipOut, File file, File targetDirectory) throws IOException {
218+
private void addFileToZipArtifact(ZipOutputStream zipOut, File file, File targetDirectory) throws IOException {
191219
if (file.exists()) {
192220
File entryName;
193-
194221
if (targetDirectory == null || targetDirectory.getName().isEmpty()) {
195222
entryName = new File(file.getName());
196223
}
@@ -210,10 +237,8 @@ private void addFileToZip(ZipOutputStream zipOut, File file, File targetDirector
210237
}
211238

212239
private void attachArtifact() {
213-
File zipFile = new File(outputDirectory, zipFilename);
214-
240+
File zipFile = new File(outputDirectory, zipArtifactFilename);
215241
getLog().info("Attaching artifact: " + zipFile.getName());
216-
217242
projectHelper.attachArtifact(project, "zip", "reqstool", zipFile);
218243
}
219244

‎src/test/java/se/lfv/reqstool/plugins/maven/RequirementsToolMojoTests.java‎

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ void testCombine() throws IOException {
2727
File testsAnnotationsFile = new File(classLoader.getResource("yml/svcs_annotations.yml").getFile());
2828

2929
JsonNode implementationsNode = RequirementsToolMojo.yamlMapper.readTree(implementationsAnnotationsFile)
30-
.path(RequirementsToolMojo.REQUIREMENT_ANNOTATIONS)
31-
.path(RequirementsToolMojo.IMPLEMENTATIONS);
30+
.path(RequirementsToolMojo.XML_REQUIREMENT_ANNOTATIONS)
31+
.path(RequirementsToolMojo.XML_IMPLEMENTATIONS);
3232

3333
JsonNode testsNode = RequirementsToolMojo.yamlMapper.readTree(testsAnnotationsFile)
34-
.path(RequirementsToolMojo.REQUIREMENT_ANNOTATIONS)
35-
.path(RequirementsToolMojo.TESTS);
34+
.path(RequirementsToolMojo.XML_REQUIREMENT_ANNOTATIONS)
35+
.path(RequirementsToolMojo.XML_TESTS);
3636

3737
JsonNode combinedNode = RequirementsToolMojo.combineOutput(implementationsNode, testsNode);
3838

@@ -47,43 +47,39 @@ void testCombine() throws IOException {
4747
}
4848

4949
@Test
50-
void testCreateZip() throws Exception {
50+
void testAssembleZipArtifact() throws Exception {
5151

5252
ClassLoader classLoader = getClass().getClassLoader();
5353
URL resourcePath = classLoader.getResource("zip");
5454
Path zipResourcePath = Paths.get(resourcePath.getFile());
55-
File file = zipResourcePath.toFile();
56-
String zipFileNameString = "reqstool-test-zip";
55+
String zipArtifactFilename = "reqstool-test-zip";
5756

5857
RequirementsToolMojo mojo = new RequirementsToolMojo();
5958

60-
Field outputDirectory = RequirementsToolMojo.class.getDeclaredField("outputDirectory");
61-
Field datasetPath = RequirementsToolMojo.class.getDeclaredField("datasetPath");
62-
Field zipFileName = RequirementsToolMojo.class.getDeclaredField("zipFilename");
63-
Field failsafeReportsDir = RequirementsToolMojo.class.getDeclaredField("failsafeReportsDir");
64-
Field surefireReportsDir = RequirementsToolMojo.class.getDeclaredField("surefireReportsDir");
59+
Field outputDirectoryField = RequirementsToolMojo.class.getDeclaredField("outputDirectory");
60+
Field datasetPathField = RequirementsToolMojo.class.getDeclaredField("datasetPath");
61+
Field zipArtifactFilenameField = RequirementsToolMojo.class.getDeclaredField("zipArtifactFilename");
62+
Field failsafeReportsDirField = RequirementsToolMojo.class.getDeclaredField("failsafeReportsDir");
63+
Field surefireReportsDirField = RequirementsToolMojo.class.getDeclaredField("surefireReportsDir");
6564

66-
outputDirectory.setAccessible(true);
67-
datasetPath.setAccessible(true);
68-
zipFileName.setAccessible(true);
69-
failsafeReportsDir.setAccessible(true);
70-
surefireReportsDir.setAccessible(true);
65+
outputDirectoryField.setAccessible(true);
66+
datasetPathField.setAccessible(true);
67+
zipArtifactFilenameField.setAccessible(true);
68+
failsafeReportsDirField.setAccessible(true);
69+
surefireReportsDirField.setAccessible(true);
7170

72-
outputDirectory.set(mojo, file);
73-
datasetPath.set(mojo, file);
74-
zipFileName.set(mojo, zipFileNameString);
75-
failsafeReportsDir.set(mojo, file);
76-
surefireReportsDir.set(mojo, file);
71+
outputDirectoryField.set(mojo, zipResourcePath.toFile());
72+
datasetPathField.set(mojo, zipResourcePath.toFile());
73+
failsafeReportsDirField.set(mojo, zipResourcePath.toFile());
74+
surefireReportsDirField.set(mojo, zipResourcePath.toFile());
75+
zipArtifactFilenameField.set(mojo, zipArtifactFilename);
7776

78-
Method zipMethod = RequirementsToolMojo.class.getDeclaredMethod("createZipFile");
79-
zipMethod.setAccessible(true);
77+
Method assembleZipArtifactMethod = RequirementsToolMojo.class.getDeclaredMethod("assembleZipArtifact");
78+
assembleZipArtifactMethod.setAccessible(true);
8079

81-
zipMethod.invoke(mojo);
80+
assembleZipArtifactMethod.invoke(mojo);
8281

83-
Path zipfileTargetPath = Paths.get("target", "test-classes", "zip");
84-
Path expectedZipFilePath = zipfileTargetPath.resolve(zipFileNameString);
85-
86-
assertTrue(Files.exists(expectedZipFilePath));
82+
assertTrue(Files.exists(zipResourcePath.resolve(zipArtifactFilename)));
8783

8884
}
8985

0 commit comments

Comments
 (0)