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.api.cli;
20
21 import org.apache.maven.api.annotations.Experimental;
22 import org.apache.maven.api.annotations.Nonnull;
23
24 /**
25 * Defines the contract for a component responsible for executing a Maven tool
26 * using the information provided in an {@link ExecutorRequest}. This interface is central
27 * to the execution of Maven commands and builds, but it does not construct nor fully parses arguments.
28 *
29 * @since 4.0.0
30 */
31 @Experimental
32 public interface Executor extends AutoCloseable {
33 // Logic borrowed from Commons-Lang3
34 boolean IS_WINDOWS = System.getProperty("os.name", "unknown").startsWith("Windows");
35
36 /**
37 * Maven version string returned when the actual version of Maven cannot be determined.
38 */
39 String UNKNOWN_VERSION = "unknown";
40
41 /**
42 * Invokes the tool application using the provided {@link ExecutorRequest}.
43 * This method is responsible for executing the command or build
44 * process based on the information contained in the request.
45 *
46 * @param executorRequest the request containing all necessary information for the execution
47 * @return an integer representing the exit code of the execution (0 typically indicates success)
48 * @throws ExecutorException if an error occurs during the execution process
49 */
50 int execute(@Nonnull ExecutorRequest executorRequest) throws ExecutorException;
51
52 /**
53 * Returns the Maven version that provided {@link ExecutorRequest} point at (would use). Please note, that this
54 * operation, depending on underlying implementation may be costly. If caller use this method often, it is
55 * caller responsibility to properly cache returned values (key can be {@link ExecutorRequest#installationDirectory()}.
56 *
57 * @param executorRequest the request containing all necessary information for the execution
58 * @return a string representing the Maven version or {@link #UNKNOWN_VERSION}
59 * @throws ExecutorException if an error occurs during the execution process
60 */
61 @Nonnull
62 String mavenVersion(@Nonnull ExecutorRequest executorRequest) throws ExecutorException;
63
64 /**
65 * Closes and disposes of this {@link Executor} instance, releasing any resources it may hold.
66 * This method is called automatically when using try-with-resources statements.
67 *
68 * <p>The default implementation does nothing. Subclasses should override this method
69 * if they need to perform cleanup operations.</p>
70 *
71 * @throws ExecutorException if an error occurs while closing the {@link Executor}
72 */
73 @Override
74 default void close() throws ExecutorException {}
75 }