/*
 * Copyright 2019. the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ms.dew.devops.maven.function;

import ms.dew.devops.kernel.exception.GlobalProcessException;
import ms.dew.devops.kernel.util.ExecuteOnceProcessor;
import org.apache.maven.DefaultProjectDependenciesResolver;
import org.apache.maven.ProjectDependenciesResolver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

import java.util.Collections;
import java.util.Set;

/**
 * 依赖发现.
 *
 * @author gudaoxuri
 */
public class DependenciesResolver {

    /**
     * Init.
     *
     * @param mavenSession the maven session
     */
    public static void init(MavenSession mavenSession) {
        if (ExecuteOnceProcessor.executedCheck(DependenciesResolver.class)) {
            return;
        }
        try {
            DefaultProjectDependenciesResolver resolver = (DefaultProjectDependenciesResolver) mavenSession.getContainer()
                    .lookup(ProjectDependenciesResolver.class.getName());
            for (MavenProject mavenProject : mavenSession.getProjectDependencyGraph().getSortedProjects()) {
                Set<Artifact> result = resolver.resolve(mavenProject, Collections.singleton(Artifact.SCOPE_COMPILE), mavenSession);
                mavenProject.setArtifacts(result);
            }
        } catch (ArtifactResolutionException | ArtifactNotFoundException | ComponentLookupException e) {
            throw new GlobalProcessException(e.getMessage(), e);
        }
    }

}

@@ -1054,32 +1054,110 @@
 utionException;%0A
+import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;%0A
 import org.apach
@@ -1349,16 +1349,69 @@
 ctions;%0A
+import java.util.HashSet;%0Aimport java.util.Optional;%0A
 import j
@@ -2089,279 +2089,1665 @@
     
-Set%3CArtifact%3E result = resolver.resolve(mavenProject, Collections.singleton(Artifact.SCOPE_COMPILE), mavenSession);%0A                mavenProject.setArtifacts(result);%0A            %7D%0A        %7D catch (ArtifactResolutionException %7C ArtifactNotFoundException %7C ComponentLookup
+mavenProject.setArtifacts(resolve(resolver, mavenProject, mavenSession));%0A            %7D%0A        %7D catch (ComponentLookupException e) %7B%0A            throw new GlobalProcessException(e.getMessage(), e);%0A        %7D%0A    %7D%0A%0A    private static Set%3CArtifact%3E resolve(DefaultProjectDependenciesResolver resolver,%0A                                         MavenProject mavenProject, MavenSession mavenSession) %7B%0A        try %7B%0A            return resolver.resolve(mavenProject, Collections.singleton(Artifact.SCOPE_COMPILE), mavenSession);%0A        %7D catch (MultipleArtifactsNotFoundException e) %7B%0A            Set%3CArtifact%3E result = new HashSet%3C%3E();%0A            for (Artifact missArtifact : e.getMissingArtifacts()) %7B%0A                Optional%3CMavenProject%3E innerProjectOpt = mavenSession.getProjects()%0A                        .stream().filter(project -%3E project.getId().equalsIgnoreCase(missArtifact.getId()))%0A                        .findAny();%0A                if (innerProjectOpt.isPresent()) %7B%0A                    result.addAll(innerProjectOpt.get().getArtifacts());%0A                %7D else %7B%0A                    throw new GlobalProcessException(e.getMessage(), e);%0A                %7D%0A            %7D%0A            try %7B%0A                result.addAll(resolver.resolve(mavenProject, null,%0A                        Collections.singleton(Artifact.SCOPE_COMPILE), mavenSession, new HashSet%3C%3E(e.getMissingArtifacts())));%0A                return result;%0A            %7D catch (ArtifactResolutionException %7C ArtifactNotFoundException ex) %7B%0A                throw new GlobalProcessException(e.getMessage(), e);%0A            %7D%0A        %7D catch (ArtifactNotFoundException %7C ArtifactResolution
 Exce

Resolve the issue of finding a new version dependency