View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.internal.ant.tasks;
20  
21  import org.apache.maven.resolver.internal.ant.AntRepoSys;
22  import org.apache.maven.resolver.internal.ant.types.Dependencies;
23  import org.apache.maven.resolver.internal.ant.types.LocalRepository;
24  import org.apache.maven.resolver.internal.ant.types.RemoteRepositories;
25  import org.apache.maven.resolver.internal.ant.types.RemoteRepository;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.types.Reference;
29  import org.eclipse.aether.collection.CollectResult;
30  
31  /**
32   * Abstract base class for Ant tasks that perform dependency resolution using Maven Resolver (Aether).
33   * <p>
34   * This class encapsulates support for specifying and configuring dependencies, remote repositories,
35   * and the local repository used during resolution.
36   * It provides helper methods to collect dependencies and manage nested configuration elements.
37   * </p>
38   *
39   * <p>This class is intended to be extended by concrete tasks that require dependency resolution,
40   * such as retrieving artifacts, resolving transitive dependencies, or analyzing dependency graphs.</p>
41   *
42   */
43  public abstract class AbstractResolvingTask extends Task {
44  
45      /**
46       * The dependency definitions to resolve.
47       * Configurable via a nested {@code <dependencies>} element or a reference using {@link #setDependenciesRef(Reference)}.
48       */
49      protected Dependencies dependencies;
50  
51      /**
52       * The list of remote repositories to use for resolution.
53       * Populated via one or more {@code <remoteRepo>} or {@code <remoteRepos>} nested elements.
54       */
55      protected RemoteRepositories remoteRepositories;
56  
57      /**
58       * Optional custom local repository definition.
59       * Created using the {@code <localRepo>} nested element.
60       */
61      protected LocalRepository localRepository;
62  
63      /**
64       * Default constructor for {@code AbstractResolvingTask}.
65       */
66      public AbstractResolvingTask() {
67          // Default constructor
68      }
69  
70      /**
71       * Adds a {@code <dependencies>} element to define the dependencies to be resolved.
72       *
73       * @param dependencies the {@link Dependencies} element to add
74       * @throws BuildException if multiple {@code <dependencies>} elements are specified
75       */
76      public void addDependencies(final Dependencies dependencies) {
77          if (this.dependencies != null) {
78              throw new BuildException("You must not specify multiple <dependencies> elements");
79          }
80          this.dependencies = dependencies;
81      }
82  
83      /**
84       * Sets a reference to an existing {@link Dependencies} instance using {@code refid}.
85       *
86       * @param ref the reference to a {@code Dependencies} instance
87       */
88      public void setDependenciesRef(final Reference ref) {
89          if (dependencies == null) {
90              dependencies = new Dependencies();
91              dependencies.setProject(getProject());
92          }
93          dependencies.setRefid(ref);
94      }
95  
96      /**
97       * Creates a {@code <localRepo>} element to specify a custom local repository for resolution.
98       *
99       * @return the created {@link LocalRepository} instance
100      * @throws BuildException if multiple {@code <localRepo>} elements are specified
101      */
102     public LocalRepository createLocalRepo() {
103         if (localRepository != null) {
104             throw new BuildException("You must not specify multiple <localRepo> elements");
105         }
106         localRepository = new LocalRepository(this);
107         return localRepository;
108     }
109 
110     /**
111      * Returns the {@link RemoteRepositories} instance used for resolution, creating it if necessary.
112      *
113      * @return the {@code RemoteRepositories} instance
114      */
115     private RemoteRepositories getRemoteRepos() {
116         if (remoteRepositories == null) {
117             remoteRepositories = new RemoteRepositories();
118             remoteRepositories.setProject(getProject());
119         }
120         return remoteRepositories;
121     }
122 
123     /**
124      * Adds a single {@code <remoteRepo>} element to the list of remote repositories used for resolution.
125      *
126      * @param repository the remote repository to add
127      */
128     public void addRemoteRepo(final RemoteRepository repository) {
129         getRemoteRepos().addRemoterepo(repository);
130     }
131 
132     /**
133      * Adds a {@code <remoteRepos>} element, representing a collection of remote repositories.
134      *
135      * @param repositories the remote repositories to add
136      */
137     public void addRemoteRepos(final RemoteRepositories repositories) {
138         getRemoteRepos().addRemoterepos(repositories);
139     }
140 
141     /**
142      * Sets a reference to an existing {@link RemoteRepositories} instance using {@code refid}.
143      *
144      * @param ref the reference to a {@code RemoteRepositories} element
145      */
146     public void setRemoteReposRef(final Reference ref) {
147         final RemoteRepositories repos = new RemoteRepositories();
148         repos.setProject(getProject());
149         repos.setRefid(ref);
150         getRemoteRepos().addRemoterepos(repos);
151     }
152 
153     /**
154      * Performs dependency collection using the configured {@link Dependencies},
155      * {@link LocalRepository}, and {@link RemoteRepositories}.
156      *
157      * @return the result of the dependency collection
158      * @throws BuildException if dependency collection fails
159      */
160     protected CollectResult collectDependencies() {
161         return AntRepoSys.getInstance(getProject())
162                 .collectDependencies(this, dependencies, localRepository, remoteRepositories);
163     }
164 }