ResolveMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { File completeAppDirectory = new File( this.project.getBuild().getOutputDirectory()); Set<Artifact> artifacts = new HashSet<> (); if( this.project.getDependencyArtifacts() != null ) artifacts.addAll( this.project.getDependencyArtifacts()); for( Artifact unresolvedArtifact : artifacts ) { String artifactId = unresolvedArtifact.getArtifactId(); String groupId = unresolvedArtifact.getGroupId(); org.eclipse.aether.artifact.Artifact aetherArtifact = new DefaultArtifact( groupId, artifactId, unresolvedArtifact.getClassifier(), unresolvedArtifact.getType(), unresolvedArtifact.getVersion()); ArtifactRequest req = new ArtifactRequest().setRepositories( this.repositories ).setArtifact( aetherArtifact ); ArtifactResult resolutionResult; try { resolutionResult = this.repoSystem.resolveArtifact( this.repoSession, req ); } catch( ArtifactResolutionException e ) { throw new MojoExecutionException( "Artifact " + artifactId + " could not be resolved.", e ); } File file = resolutionResult.getArtifact().getFile(); if( file == null || ! file.exists()) { getLog().warn( "Artifact " + artifactId + " has no attached file. Its content will not be copied in the target model directory." ); continue; } File targetDirectory = new File( completeAppDirectory, Constants.PROJECT_DIR_GRAPH + "/" + groupId + "/" + artifactId ); getLog().debug( "Copying the content of artifact " + artifactId + " under " + targetDirectory ); try { Utils.extractZipArchive( file, targetDirectory, "graph/[^/]*\\.graph", "graph/" ); Utils.extractZipArchive( file, targetDirectory.getParentFile(), "graph/.*/.*", "graph/" ); } catch( IOException e ) { throw new MojoExecutionException( "The ZIP archive for artifact " + artifactId + " could not be extracted.", e ); } File temporaryDirectory = new File( System.getProperty( "java.io.tmpdir" ), "roboconf-temp" ); try { Utils.extractZipArchive( file, temporaryDirectory ); ApplicationLoadResult alr = RuntimeModelIo.loadApplicationFlexibly( temporaryDirectory ); if( alr.getApplicationTemplate().getGraphs() != null ) { for( File dir : ResourceUtils.findScopedInstancesDirectories( alr.getApplicationTemplate()).values()) { if( Utils.listAllFiles( dir ).isEmpty()) continue; getLog().warn( "Artifact " + artifactId + " contains target properties. Reusable target properties should be packaged separately." ); } } } catch( IOException e ) { getLog().debug( "The presence of target properties in the " + artifactId + " artifact could not be verified." ); } finally { Utils.deleteFilesRecursivelyAndQuietly( temporaryDirectory ); } } }  @Override void execute();  }
@Test public void testNoDependency() throws Exception { final String projectName = "project--valid"; File baseDir = this.resources.getBasedir( projectName ); Assert.assertNotNull( baseDir ); Assert.assertTrue( baseDir.isDirectory()); File targetDir = new File( baseDir, MavenPluginConstants.TARGET_MODEL_DIRECTORY + "/" + Constants.PROJECT_DIR_GRAPH ); Assert.assertFalse( targetDir.isDirectory()); findMojo( projectName, "resolve" ).execute(); Assert.assertFalse( targetDir.isDirectory()); }
@Test( expected = MojoExecutionException.class ) public void testWithInvalidRoboconfDependencies() throws Exception { final String projectName = "project--valid"; File baseDir = this.resources.getBasedir( projectName ); Assert.assertNotNull( baseDir ); Assert.assertTrue( baseDir.isDirectory()); AbstractMojo mojo = findMojo( projectName, "resolve" ); this.rule.setVariableValueToObject( mojo, "repoSystem", newRepositorySystem()); this.rule.setVariableValueToObject( mojo, "repositories", new ArrayList<RemoteRepository>( 0 )); MavenProject project = (MavenProject) this.rule.getVariableValueFromObject( mojo, "project" ); project.setDependencyArtifacts( new HashSet<Artifact> ()); Artifact notRbcfArtifact1 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler()); project.getDependencyArtifacts().add( notRbcfArtifact1 ); Artifact notRbcfArtifact2 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler()); notRbcfArtifact2.setFile( new File( "file that does not exist" )); project.getDependencyArtifacts().add( notRbcfArtifact2 ); Artifact notRbcfArtifact3 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler()); File temp = this.folder.newFile( "toto.zip" ); Assert.assertTrue( temp.exists()); notRbcfArtifact3.setFile( temp ); project.getDependencyArtifacts().add( notRbcfArtifact3 ); File targetDir = new File( baseDir, MavenPluginConstants.TARGET_MODEL_DIRECTORY + "/" + Constants.PROJECT_DIR_GRAPH ); Assert.assertFalse( targetDir.isDirectory()); mojo.execute(); }
@Test public void testWithValidRoboconfDependencies() throws Exception { final String projectName = "project--valid"; File baseDir = this.resources.getBasedir( projectName ); Assert.assertNotNull( baseDir ); Assert.assertTrue( baseDir.isDirectory()); AbstractMojo mojo = findMojo( projectName, "resolve" ); this.rule.setVariableValueToObject( mojo, "repoSystem", newRepositorySystem()); this.rule.setVariableValueToObject( mojo, "repositories", new ArrayList<RemoteRepository>( 0 )); File dir = this.folder.newFolder(); File targetZipFile = this.folder.newFile(); Assert.assertTrue( targetZipFile.delete()); CreationBean bean = new CreationBean() .projectDescription( "some desc" ).projectName( "my-project" ) .groupId( "net.roboconf" ).projectVersion( "1.0-SNAPSHOT" ).mavenProject( false ); ProjectUtils.createProjectSkeleton( dir, bean ); ZipArchiver zipArchiver = new ZipArchiver(); zipArchiver.addDirectory( dir ); zipArchiver.setCompress( true ); zipArchiver.setDestFile( targetZipFile ); zipArchiver.createArchive(); Assert.assertTrue( targetZipFile.isFile()); MavenProject project = (MavenProject) this.rule.getVariableValueFromObject( mojo, "project" ); project.setDependencyArtifacts( new HashSet<Artifact> ()); Artifact rbcfArtifact3 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler()); rbcfArtifact3.setFile( targetZipFile ); project.getDependencyArtifacts().add( rbcfArtifact3 ); this.artifactIdToArtifact.put( rbcfArtifact3.getArtifactId(), rbcfArtifact3 ); File targetDir = new File( baseDir, MavenPluginConstants.TARGET_MODEL_DIRECTORY + "/" + Constants.PROJECT_DIR_GRAPH ); Assert.assertFalse( targetDir.isDirectory()); mojo.execute(); File importDir = new File( targetDir, "net.roboconf/roboconf-core" ); Assert.assertTrue( importDir.isDirectory()); Assert.assertTrue( new File( importDir, "main.graph" ).isFile()); }