/**
* Resolve the executable dependencies for the specified project
*
* @param executablePomArtifact the project's POM
* @return a set of Artifacts
* @throws MojoExecutionException if a failure happens
*/
private Set<Artifact> resolveExecutableDependencies( Artifact executablePomArtifact )
throws MojoExecutionException
{
Set<Artifact> executableDependencies = new LinkedHashSet<>();
try
{
ProjectBuildingRequest buildingRequest = getSession().getProjectBuildingRequest();
MavenProject executableProject =
this.projectBuilder.build( executablePomArtifact, buildingRequest ).getProject();
for ( ArtifactResult artifactResult : dependencyResolver.resolveDependencies( buildingRequest, executableProject.getModel(), null ) )
{
executableDependencies.add( artifactResult.getArtifact() );
}
}
catch ( Exception ex )
{
throw new MojoExecutionException( "Encountered problems resolving dependencies of the executable "
+ "in preparation for its execution.", ex );
}
return executableDependencies;
}
private ArtifactResult doResolve(final ArtifactResolver resolver, final Artifact art, final MavenSession session)
throws ArtifactResolverException {
final ProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
session
.getTopLevelProject()
.getPluginRepositories()
.stream()
.map(this::toArtifactRepo)
.filter(it -> request
.getPluginArtifactRepositories()
.stream()
.noneMatch(a -> String.valueOf(a.getId()).equalsIgnoreCase(it.getId())))
.forEach(it -> request.getRemoteRepositories().add(it));
return resolver.resolveArtifact(request, art);
}
/**
* Scan project dependencies to find artifacts generated by mvn golang
* project.
*
* @param mavenProject maven project, must not be null
* @param includeTestDependencies flag to process dependencies marked for test
* phases
* @param mojo calling mojo, must not be null
* @param session maven session, must not be null
* @param execution maven execution, must not be null
* @param resolver artifact resolver, must not be null
* @param remoteRepositories list of remote repositories, must not be null
* @return list of files found in artifacts generated by mvn golang plugin
* @throws ArtifactResolverException exception thrown if some artifact can't
* be resolved
*/
@Nonnull
@MustNotContainNull
public static List<Tuple<Artifact, File>> scanForMvnGoArtifacts(
@Nonnull final MavenProject mavenProject,
final boolean includeTestDependencies,
@Nonnull final AbstractMojo mojo,
@Nonnull final MavenSession session,
@Nonnull final MojoExecution execution,
@Nonnull final ArtifactResolver resolver,
@Nonnull @MustNotContainNull final List<ArtifactRepository> remoteRepositories
) throws ArtifactResolverException {
final List<Tuple<Artifact, File>> result = new ArrayList<>();
// final String phase = execution.getLifecyclePhase();
final Set<String> alreadyFoundArtifactRecords = new HashSet<>();
MavenProject currentProject = mavenProject;
while (currentProject != null && !Thread.currentThread().isInterrupted()) {
final Set<Artifact> projectDependencies = currentProject.getDependencyArtifacts();
final List<Artifact> artifacts = new ArrayList<>(projectDependencies == null ? Collections.emptySet() : projectDependencies);
mojo.getLog().debug("Detected dependency artifacts: " + artifacts);
while (!artifacts.isEmpty() && !Thread.currentThread().isInterrupted()) {
final Artifact artifact = artifacts.remove(0);
if (Artifact.SCOPE_TEST.equals(artifact.getScope()) && !includeTestDependencies) {
continue;
}
if (artifact.getType().equals(AbstractGolangMojo.GOARTIFACT_PACKAGING)) {
final ArtifactResult artifactResult = resolver.resolveArtifact(makeResolveArtifactProjectBuildingRequest(session, remoteRepositories), artifact);
final File zipFillePath = artifactResult.getArtifact().getFile();
mojo.getLog().debug("Detected MVN-GOLANG marker inside ZIP dependency: " + artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getVersion() + ':' + artifact.getType());
if (ZipUtil.containsEntry(zipFillePath, GolangMvnInstallMojo.MVNGOLANG_DEPENDENCIES_FILE)) {
final byte[] artifactFlagFile = ZipUtil.unpackEntry(zipFillePath, GolangMvnInstallMojo.MVNGOLANG_DEPENDENCIES_FILE, StandardCharsets.UTF_8);
for (final String str : new String(artifactFlagFile, StandardCharsets.UTF_8).split("\\R")) {
if (str.trim().isEmpty() || alreadyFoundArtifactRecords.contains(str)) {
continue;
}
mojo.getLog().debug("Adding mvn-golang dependency: " + str);
alreadyFoundArtifactRecords.add(str);
try {
artifacts.add(parseArtifactRecord(str, new MvnGolangArtifactHandler()));
} catch (InvalidVersionSpecificationException ex) {
throw new ArtifactResolverException("Can't make artifact: " + str, ex);
}
}
}
final File artifactFile = artifactResult.getArtifact().getFile();
mojo.getLog().debug("Artifact file: " + artifactFile);
if (doesContainFile(result, artifactFile)) {
mojo.getLog().debug("Artifact file ignored as duplication: " + artifactFile);
} else {
result.add(Tuple.of(artifact, artifactFile));
}
}
}
currentProject = currentProject.hasParent() ? currentProject.getParent() : null;
}
return result;
}