PackageApplicationMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { File modelDir = new File( this.project.getBuild().getOutputDirectory()); if( ! modelDir.exists()) throw new MojoExecutionException( "The model could not be found. " + modelDir ); File targetDir = new File( this.project.getBuild().getDirectory()); String archiveName = this.project.getBuild().getFinalName() + ".zip"; File archiveFile = new File( targetDir, archiveName ); try { this.zipArchiver.addDirectory( modelDir ); this.zipArchiver.setCompress( true ); this.zipArchiver.setDestFile( archiveFile ); this.zipArchiver.createArchive(); this.project.getArtifact().setFile( archiveFile ); } catch( Exception e ) { throw new MojoExecutionException( "Exception while packaging.", e ); } }  @Override void execute();  }
@Test public void testValidAppProject() throws Exception { final String finalName = "output"; final String version = "1"; File targetDirectory = this.resources.getBasedir( "project--valid" ); File targetArchive = new File( targetDirectory, "target/" + finalName + ".zip" ); File modelDirectory = new File( targetDirectory, MavenPluginConstants.TARGET_MODEL_DIRECTORY ); Assert.assertFalse( targetArchive.exists()); Assert.assertFalse( modelDirectory.exists()); File pom = new File( targetDirectory, "pom.xml" ); final MavenProject mvnProject = new MavenProject() ; mvnProject.setFile( pom ) ; mvnProject.setVersion( version ); mvnProject.getBuild().setDirectory( modelDirectory.getParentFile().getAbsolutePath()); mvnProject.getBuild().setOutputDirectory( modelDirectory.getAbsolutePath()); mvnProject.getBuild().setFinalName( finalName ); mvnProject.setArtifact( new ProjectArtifact( mvnProject )); Utils.copyDirectory( new File( mvnProject.getBasedir(), MavenPluginConstants.SOURCE_MODEL_DIRECTORY ), new File( mvnProject.getBuild().getOutputDirectory())); PackageApplicationMojo packageApplicationMojo = (PackageApplicationMojo) this.rule.lookupMojo( "package-application", pom ); this.rule.setVariableValueToObject( packageApplicationMojo, "project", mvnProject ); packageApplicationMojo.execute(); Assert.assertTrue( targetArchive.exists()); targetDirectory = this.folder.newFolder(); Utils.extractZipArchive( targetArchive, targetDirectory ); ApplicationLoadResult alr = RuntimeModelIo.loadApplication( targetDirectory ); Assert.assertEquals( 0, alr.getLoadErrors().size()); Assert.assertEquals( "1.0.0", alr.getApplicationTemplate().getVersion()); File notFilteredFile = new File( targetDirectory, "graph/Tomcat/readme.md" ); Assert.assertTrue( notFilteredFile.exists()); String content = Utils.readFileContent( notFilteredFile ); Assert.assertTrue( content.contains( "${project.version}" )); Assert.assertFalse( content.contains( "1.0-SNAPSHOT" )); }
@Test( expected = MojoExecutionException.class ) public void testInvalidAppProject() throws Exception { final String finalName = "output"; final String version = "1"; File targetDirectory = this.resources.getBasedir( "project--valid" ); File targetArchive = new File( targetDirectory, "target/" + finalName + ".zip" ); File modelDirectory = new File( targetDirectory, MavenPluginConstants.TARGET_MODEL_DIRECTORY ); Assert.assertFalse( targetArchive.exists()); Assert.assertFalse( modelDirectory.exists()); File pom = new File( targetDirectory, "pom.xml" ); final MavenProject mvnProject = new MavenProject() ; mvnProject.setFile( pom ) ; mvnProject.setVersion( version ); mvnProject.getBuild().setDirectory( modelDirectory.getAbsolutePath()); mvnProject.getBuild().setOutputDirectory( modelDirectory.getParentFile().getAbsolutePath()); mvnProject.getBuild().setFinalName( finalName ); mvnProject.setArtifact( new ProjectArtifact( mvnProject )); PackageApplicationMojo packageApplicationMojo = (PackageApplicationMojo) this.rule.lookupMojo( "package-application", pom ); this.rule.setVariableValueToObject( packageApplicationMojo, "project", mvnProject ); packageApplicationMojo.execute(); }