DefaultTargetPlatformConfigurationReader { protected void addTargetArtifact(TargetPlatformConfiguration result, MavenSession session, MavenProject project, Xpp3Dom artifactDom) throws MojoExecutionException { Xpp3Dom groupIdDom = artifactDom.getChild("groupId"); Xpp3Dom artifactIdDom = artifactDom.getChild("artifactId"); Xpp3Dom versionDom = artifactDom.getChild("version"); if (groupIdDom == null || artifactIdDom == null || versionDom == null) { throw new BuildFailureException( "The target artifact configuration is invalid - <groupId>, <artifactId> and <version> are mandatory"); } Xpp3Dom classifierDom = artifactDom.getChild("classifier"); String groupId = groupIdDom.getValue(); String artifactId = artifactIdDom.getValue(); String version = versionDom.getValue(); String classifier = classifierDom != null ? classifierDom.getValue() : null; for (MavenProject otherProject : session.getProjects()) { if (groupId.equals(otherProject.getGroupId()) && artifactId.equals(otherProject.getArtifactId()) && version.equals(otherProject.getVersion())) { if (classifier == null) { File[] targetFiles = listTargetFiles(otherProject.getBasedir()); for (File targetFile : targetFiles) { if (isPrimaryTarget(otherProject, targetFile, targetFiles)) { result.addTarget(targetFile); return; } } throwNoPrimaryTargetFound(otherProject, targetFiles); } else { File target = new File(otherProject.getBasedir(), classifier + FILE_EXTENSION); if (isTargetFile(target)) { result.addTarget(target); return; } else { throw new MojoExecutionException("target definition file '" + target + "' not found in project '" + project.getName() + "'."); } } } } Artifact artifact = repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, TARGET, classifier); ArtifactResolutionRequest request = new ArtifactResolutionRequest(); request.setArtifact(artifact); request.setLocalRepository(session.getLocalRepository()); request.setRemoteRepositories(project.getRemoteArtifactRepositories()); repositorySystem.resolve(request); if (artifact.isResolved()) { result.addTarget(artifact.getFile()); return; } throw new RuntimeException("Could not resolve target platform specification artifact " + artifact); }  TargetPlatformConfiguration getTargetPlatformConfiguration(MavenSession session, MavenProject project); static File[] listTargetFiles(File folder); static boolean isTargetFile(File file); static boolean isPrimaryTarget(MavenProject project, File targetFile, File[] otherTargetFiles); static void throwNoPrimaryTargetFound(MavenProject project, File[] targetFiles); static final String TARGET_DEFINITION_INCLUDE_SOURCE; static final String INCLUDE_PACKED_ARTIFACTS; static final String DEPENDENCY_RESOLUTION; static final String FILTERS; static final String RESOLVE_WITH_EXECUTION_ENVIRONMENT_CONSTRAINTS; static final String BREE_HEADER_SELECTION_POLICY; static final String EXECUTION_ENVIRONMENT_DEFAULT; static final String EXECUTION_ENVIRONMENT; static final String ALLOW_CONFLICTING_DEPENDENCIES; static final String POM_DEPENDENCIES; static final String TARGET; static final String RESOLVER; static final String ENVIRONMENTS; }
@Test() public void testAddTargetWithValidMissingTargetDefinition() { Xpp3Dom dom = createGavConfiguration("myGroupId", "myArtifactId", "myVersion"); MavenSession session = setupMockSession(); TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); try { configurationReader.addTargetArtifact(configuration, session, null, dom); } catch (MojoExecutionException e) { assertTrue(e.getMessage().contains("No target definition file(s) found in project")); } }
@Test public void testAddTargetWithMissingVersionInTargetDefinition() throws MojoExecutionException { Xpp3Dom dom = createGavConfiguration("myGroupId", "myArtifactId", null); MavenSession session = setupMockSession(); TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); try { configurationReader.addTargetArtifact(configuration, session, null, dom); fail(); } catch (BuildFailureException e) { assertTrue(e.getMessage().contains("The target artifact configuration is invalid")); } }
@Test public void testAddTargetWithMissingGroupInTargetDefinition() throws MojoExecutionException { Xpp3Dom dom = createGavConfiguration(null, "myArtifactId", "myVersion"); MavenSession session = setupMockSession(); TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); try { configurationReader.addTargetArtifact(configuration, session, null, dom); fail(); } catch (BuildFailureException e) { assertTrue(e.getMessage().contains("The target artifact configuration is invalid")); } }
@Test public void testAddTargetWithMissingArtifactIdInTargetDefinition() throws MojoExecutionException { Xpp3Dom dom = createGavConfiguration("myGroupId", null, "myVersion"); MavenSession session = setupMockSession(); TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); try { configurationReader.addTargetArtifact(configuration, session, null, dom); fail(); } catch (BuildFailureException e) { assertTrue(e.getMessage().contains("The target artifact configuration is invalid")); } }