Skip to content

Commit fcd9c0f

Browse files
authored
[MNG-8385] Introduce proto session, make CLIng use PropertyContributor (#1929)
And make use of it in CLIng. Also, move from "late" Resolver session factory to "early" CLIng invocation of `PropertyContributor` SPI and make contribution visible across whole Maven, not only Resolver. --- https://issues.apache.org/jira/browse/MNG-8385
1 parent 5c02857 commit fcd9c0f

File tree

36 files changed

+584
-1794
lines changed

36 files changed

+584
-1794
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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;
20+
21+
import java.nio.file.Path;
22+
import java.time.Instant;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import org.apache.maven.api.annotations.Experimental;
27+
import org.apache.maven.api.annotations.Nonnull;
28+
import org.apache.maven.api.annotations.Nullable;
29+
import org.apache.maven.api.annotations.ThreadSafe;
30+
31+
import static java.util.Objects.requireNonNull;
32+
33+
/**
34+
* The proto session, material used to create {@link Session}.
35+
*
36+
* @since 4.0.0
37+
*/
38+
@Experimental
39+
@ThreadSafe
40+
public interface ProtoSession {
41+
42+
/**
43+
* Returns immutable user properties to use for interpolation. The user properties have been configured directly
44+
* by the user, e.g. via the {@code -Dkey=value} parameter on the command line.
45+
*
46+
* @return the user properties, never {@code null}
47+
*/
48+
@Nonnull
49+
Map<String, String> getUserProperties();
50+
51+
/**
52+
* Returns immutable system properties to use for interpolation. The system properties are collected from the
53+
* runtime environment such as {@link System#getProperties()} and environment variables
54+
* (prefixed with {@code env.}).
55+
*
56+
* @return the system properties, never {@code null}
57+
*/
58+
@Nonnull
59+
Map<String, String> getSystemProperties();
60+
61+
/**
62+
* Returns the start time of the session.
63+
*
64+
* @return the start time as an Instant object, never {@code null}
65+
*/
66+
@Nonnull
67+
Instant getStartTime();
68+
69+
/**
70+
* Gets the directory of the topmost project being built, usually the current directory or the
71+
* directory pointed at by the {@code -f/--file} command line argument.
72+
*
73+
* @return the directory of the topmost project, never {@code null}
74+
* @see Project#isTopProject()
75+
* @see #getRootDirectory()
76+
*/
77+
@Nonnull
78+
Path getTopDirectory();
79+
80+
/**
81+
* Gets the root directory of the session, which is the root directory for the top directory project.
82+
*
83+
* @return the root directory, never {@code null}
84+
* @throws IllegalStateException if the root directory could not be found
85+
* @see #getTopDirectory()
86+
* @see Project#getRootDirectory()
87+
* @see Project#isRootProject()
88+
*/
89+
@Nonnull
90+
Path getRootDirectory();
91+
92+
/**
93+
* Returns a proto session builder of this instance.
94+
*/
95+
@Nonnull
96+
default Builder toBuilder() {
97+
try {
98+
return new Builder(
99+
getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), getRootDirectory());
100+
} catch (IllegalStateException e) {
101+
return new Builder(getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), null);
102+
}
103+
}
104+
105+
/**
106+
* Returns new builder from scratch.
107+
*/
108+
static Builder newBuilder() {
109+
return new Builder().withStartTime(Instant.now());
110+
}
111+
112+
class Builder {
113+
private Map<String, String> userProperties;
114+
private Map<String, String> systemProperties;
115+
private Instant startTime;
116+
private Path topDirectory;
117+
private Path rootDirectory;
118+
119+
private Builder() {}
120+
121+
private Builder(
122+
Map<String, String> userProperties,
123+
Map<String, String> systemProperties,
124+
Instant startTime,
125+
Path topDirectory,
126+
Path rootDirectory) {
127+
this.userProperties = userProperties;
128+
this.systemProperties = systemProperties;
129+
this.startTime = startTime;
130+
this.topDirectory = topDirectory;
131+
this.rootDirectory = rootDirectory;
132+
}
133+
134+
public Builder withUserProperties(@Nonnull Map<String, String> userProperties) {
135+
this.userProperties = new HashMap<>(userProperties);
136+
return this;
137+
}
138+
139+
public Builder withSystemProperties(@Nonnull Map<String, String> systemProperties) {
140+
this.systemProperties = new HashMap<>(systemProperties);
141+
return this;
142+
}
143+
144+
public Builder withStartTime(@Nonnull Instant startTime) {
145+
this.startTime = requireNonNull(startTime, "startTime");
146+
return this;
147+
}
148+
149+
public Builder withTopDirectory(@Nonnull Path topDirectory) {
150+
this.topDirectory = requireNonNull(topDirectory, "topDirectory");
151+
return this;
152+
}
153+
154+
public Builder withRootDirectory(@Nullable Path rootDirectory) {
155+
this.rootDirectory = rootDirectory;
156+
return this;
157+
}
158+
159+
public ProtoSession build() {
160+
return new Impl(userProperties, systemProperties, startTime, topDirectory, rootDirectory);
161+
}
162+
163+
private static class Impl implements ProtoSession {
164+
private final Map<String, String> userProperties;
165+
private final Map<String, String> systemProperties;
166+
private final Instant startTime;
167+
private final Path topDirectory;
168+
private final Path rootDirectory;
169+
170+
private Impl(
171+
Map<String, String> userProperties,
172+
Map<String, String> systemProperties,
173+
Instant startTime,
174+
Path topDirectory,
175+
Path rootDirectory) {
176+
this.userProperties = requireNonNull(userProperties);
177+
this.systemProperties = requireNonNull(systemProperties);
178+
this.startTime = requireNonNull(startTime);
179+
this.topDirectory = requireNonNull(topDirectory);
180+
this.rootDirectory = rootDirectory;
181+
}
182+
183+
@Override
184+
public Map<String, String> getUserProperties() {
185+
return userProperties;
186+
}
187+
188+
@Override
189+
public Map<String, String> getSystemProperties() {
190+
return systemProperties;
191+
}
192+
193+
@Override
194+
public Instant getStartTime() {
195+
return startTime;
196+
}
197+
198+
@Override
199+
public Path getTopDirectory() {
200+
return topDirectory;
201+
}
202+
203+
@Override
204+
public Path getRootDirectory() {
205+
if (rootDirectory == null) {
206+
throw new IllegalStateException("root directory not set");
207+
}
208+
return rootDirectory;
209+
}
210+
}
211+
}
212+
}

api/maven-api-core/src/main/java/org/apache/maven/api/Session.java

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package org.apache.maven.api;
2020

2121
import java.nio.file.Path;
22-
import java.time.Instant;
2322
import java.util.Collection;
2423
import java.util.List;
2524
import java.util.Map;
@@ -46,7 +45,15 @@
4645
*/
4746
@Experimental
4847
@ThreadSafe
49-
public interface Session {
48+
public interface Session extends ProtoSession {
49+
50+
/**
51+
* Returns the current maven version.
52+
*
53+
* @return the maven version, never {@code null}
54+
*/
55+
@Nonnull
56+
Version getMavenVersion();
5057

5158
/**
5259
* Retrieves the settings for the current session.
@@ -80,25 +87,6 @@ public interface Session {
8087
@Nonnull
8188
SessionData getData();
8289

83-
/**
84-
* Returns immutable user properties to use for interpolation. The user properties have been configured directly
85-
* by the user, e.g. via the {@code -Dkey=value} parameter on the command line.
86-
*
87-
* @return the user properties, never {@code null}
88-
*/
89-
@Nonnull
90-
Map<String, String> getUserProperties();
91-
92-
/**
93-
* Returns immutable system properties to use for interpolation. The system properties are collected from the
94-
* runtime environment such as {@link System#getProperties()} and environment variables
95-
* (prefixed with {@code env.}).
96-
*
97-
* @return the system properties, never {@code null}
98-
*/
99-
@Nonnull
100-
Map<String, String> getSystemProperties();
101-
10290
/**
10391
* Each invocation computes a new map of effective properties. To be used in interpolation.
10492
* <p>
@@ -121,52 +109,13 @@ public interface Session {
121109
@Nonnull
122110
Map<String, String> getEffectiveProperties(@Nullable Project project);
123111

124-
/**
125-
* Returns the current maven version.
126-
*
127-
* @return the maven version, never {@code null}
128-
*/
129-
@Nonnull
130-
Version getMavenVersion();
131-
132112
/**
133113
* Returns the degree of concurrency for the build.
134114
*
135115
* @return the degree of concurrency
136116
*/
137117
int getDegreeOfConcurrency();
138118

139-
/**
140-
* Returns the start time of the session.
141-
*
142-
* @return the start time as an Instant object, never {@code null}
143-
*/
144-
@Nonnull
145-
Instant getStartTime();
146-
147-
/**
148-
* Gets the directory of the topmost project being built, usually the current directory or the
149-
* directory pointed at by the {@code -f/--file} command line argument.
150-
*
151-
* @return the directory of the topmost project, never {@code null}
152-
* @see Project#isTopProject()
153-
* @see #getRootDirectory()
154-
*/
155-
@Nonnull
156-
Path getTopDirectory();
157-
158-
/**
159-
* Gets the root directory of the session, which is the root directory for the top directory project.
160-
*
161-
* @return the root directory, never {@code null}
162-
* @throws IllegalStateException if the root directory could not be found
163-
* @see #getTopDirectory()
164-
* @see Project#getRootDirectory()
165-
* @see Project#isRootProject()
166-
*/
167-
@Nonnull
168-
Path getRootDirectory();
169-
170119
/**
171120
* Retrieves a list of projects associated with the session.
172121
*

api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactCoordinatesFactoryRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public ArtifactCoordinatesFactoryRequest build() {
167167
session, groupId, artifactId, version, classifier, extension, type, coordinateString);
168168
}
169169

170-
private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest
170+
private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest<Session>
171171
implements ArtifactCoordinatesFactoryRequest {
172172
private final String groupId;
173173
private final String artifactId;

api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactDeployerRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public ArtifactDeployerRequest build() {
101101
return new DefaultArtifactDeployerRequest(session, repository, artifacts, retryFailedDeploymentCount);
102102
}
103103

104-
private static class DefaultArtifactDeployerRequest extends BaseRequest implements ArtifactDeployerRequest {
104+
private static class DefaultArtifactDeployerRequest extends BaseRequest<Session>
105+
implements ArtifactDeployerRequest {
105106

106107
private final RemoteRepository repository;
107108
private final Collection<ProducedArtifact> artifacts;

api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactFactoryRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public ArtifactFactoryRequest build() {
136136
session, groupId, artifactId, version, classifier, extension, type);
137137
}
138138

139-
private static class DefaultArtifactFactoryRequest extends BaseRequest implements ArtifactFactoryRequest {
139+
private static class DefaultArtifactFactoryRequest extends BaseRequest<Session>
140+
implements ArtifactFactoryRequest {
140141
private final String groupId;
141142
private final String artifactId;
142143
private final String version;

api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactInstallerRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public ArtifactInstallerRequest build() {
8383
return new DefaultArtifactInstallerRequest(session, artifacts);
8484
}
8585

86-
static class DefaultArtifactInstallerRequest extends BaseRequest implements ArtifactInstallerRequest {
86+
static class DefaultArtifactInstallerRequest extends BaseRequest<Session> implements ArtifactInstallerRequest {
8787

8888
private final Collection<ProducedArtifact> artifacts;
8989

api/maven-api-core/src/main/java/org/apache/maven/api/services/ArtifactResolverRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public ArtifactResolverRequest build() {
106106
return new DefaultArtifactResolverRequest(session, coordinates, repositories);
107107
}
108108

109-
private static class DefaultArtifactResolverRequest extends BaseRequest implements ArtifactResolverRequest {
109+
private static class DefaultArtifactResolverRequest extends BaseRequest<Session>
110+
implements ArtifactResolverRequest {
110111
@Nonnull
111112
private final Collection<? extends ArtifactCoordinates> coordinates;
112113

api/maven-api-core/src/main/java/org/apache/maven/api/services/BaseRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.Collection;
2323
import java.util.Collections;
2424

25-
import org.apache.maven.api.Session;
25+
import org.apache.maven.api.ProtoSession;
2626
import org.apache.maven.api.annotations.Experimental;
2727
import org.apache.maven.api.annotations.Nonnull;
2828

@@ -32,16 +32,16 @@
3232
* @since 4.0.0
3333
*/
3434
@Experimental
35-
abstract class BaseRequest {
35+
abstract class BaseRequest<S extends ProtoSession> {
3636

37-
private final Session session;
37+
private final S session;
3838

39-
protected BaseRequest(@Nonnull Session session) {
39+
protected BaseRequest(@Nonnull S session) {
4040
this.session = nonNull(session, "session cannot be null");
4141
}
4242

4343
@Nonnull
44-
public Session getSession() {
44+
public S getSession() {
4545
return session;
4646
}
4747

0 commit comments

Comments
 (0)