PackageTargetMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { File outputDirectory = new File( this.project.getBuild().getOutputDirectory()); 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( outputDirectory ); 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 testValidTargetProject() throws Exception { final String finalName = "output"; final String version = "1"; File baseDirectory = this.resources.getBasedir( "target-ok-multi" ); File compileDirectory = new File( baseDirectory, "target/test/classes" ); File targetArchive = new File( baseDirectory, "target/" + finalName + ".zip" ); Assert.assertFalse( targetArchive.exists()); File pom = new File( baseDirectory, "pom.xml" ); final MavenProject mvnProject = new MavenProject() ; mvnProject.setFile( pom ) ; mvnProject.setVersion( version ); mvnProject.getBuild().setFinalName( finalName ); mvnProject.setArtifact( new ProjectArtifact( mvnProject )); mvnProject.getBuild().setDirectory( new File( baseDirectory, "target" ).getAbsolutePath()); mvnProject.getBuild().setOutputDirectory( compileDirectory.getAbsolutePath()); Utils.copyDirectory( new File( mvnProject.getBasedir(), "src/main/resources" ), new File( mvnProject.getBuild().getOutputDirectory())); PackageTargetMojo packageTargetMojo = (PackageTargetMojo) this.rule.lookupMojo( "package-target", pom ); this.rule.setVariableValueToObject( packageTargetMojo, "project", mvnProject ); packageTargetMojo.execute(); Assert.assertTrue( targetArchive.exists()); File unzipDirectory = this.folder.newFolder(); Utils.extractZipArchive( targetArchive, unzipDirectory ); List<File> files = Utils.listAllFiles( unzipDirectory ); Assert.assertEquals( 2, files.size()); Assert.assertTrue( files.contains( new File( unzipDirectory, "test1.properties" ))); Assert.assertTrue( files.contains( new File( unzipDirectory, "test2.properties" ))); }