1111import java .util .Locale ;
1212import java .util .zip .ZipEntry ;
1313import java .util .zip .ZipOutputStream ;
14+
15+ import javax .inject .Inject ;
16+
1417import org .apache .commons .io .FileUtils ;
1518import org .apache .maven .plugin .AbstractMojo ;
1619import org .apache .maven .plugin .MojoExecutionException ;
1720import org .apache .maven .plugin .logging .Log ;
18- import org .apache .maven .plugins .annotations .Component ;
1921import org .apache .maven .plugins .annotations .LifecyclePhase ;
2022import org .apache .maven .plugins .annotations .Mojo ;
2123import org .apache .maven .plugins .annotations .Parameter ;
2224import org .apache .maven .project .MavenProject ;
2325import org .apache .maven .project .MavenProjectHelper ;
26+
2427import com .fasterxml .jackson .databind .JsonNode ;
2528import com .fasterxml .jackson .databind .ObjectMapper ;
2629import com .fasterxml .jackson .databind .SerializationFeature ;
2730import com .fasterxml .jackson .databind .node .ObjectNode ;
2831import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
2932import 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 )
3235public 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
0 commit comments