-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMavenProvider.java
More file actions
152 lines (136 loc) · 5.37 KB
/
MavenProvider.java
File metadata and controls
152 lines (136 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package io.codemodder.plugins.maven;
import io.codemodder.*;
import io.codemodder.plugins.maven.operator.POMOperator;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
import org.jetbrains.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provides Maven dependency management functions to codemods.
*
* <p>Current Limitations are:
*
* <p>a. We skip parent finding if there's not a relativePath declaration (this is by design), so
* sometimes pom finding will fail on purpose b. there are several flags on ProjectModelFactory
* which aren't applied. They relate to verisons, upgrading and particularly: Actives Profiles c. If
* you need anything declared in a ~/.m2/settings.xml, we don't support that (e.g., passwords or
* proxies) d. Haven't tested, but I'm almost sure that it wouldn't work on any repo other than
* central e. We allow on this module to do online resolution. HOWEVER by default its offline f. You
* need to set an `M2_REPO` environment variable and/or property or declare withRepositoryPath to
* somewhere writable
*/
public final class MavenProvider implements ProjectProvider {
/** Represents a failure when doing a dependency update. */
static class DependencyUpdateException extends RuntimeException {
DependencyUpdateException(String message, Throwable cause) {
super(message, cause);
}
}
/** A seam for handling writing poms to disk. */
interface PomModifier {
/**
* Modifies a POM writing back its contents
*
* @param path where to write
* @param contents contents to write
* @throws IOException failure when writing
*/
void modify(final Path path, final byte[] contents) throws IOException;
}
/** Default Implementation of Pom Modifier Interface */
static class DefaultPomModifier implements PomModifier {
@Override
public void modify(final Path path, final byte[] contents) throws IOException {
Files.write(path, contents);
}
}
private final POMDependencyUpdater pomDependencyUpdater;
private final PomFileFinder pomFileFinder;
MavenProvider(
final PomModifier pomModifier,
final PomFileFinder pomFileFinder,
final DependencyDescriptor dependencyDescriptor,
final ArtifactInjectionPositionFinder positionFinder) {
Objects.requireNonNull(pomModifier);
Objects.requireNonNull(pomFileFinder);
this.pomFileFinder = pomFileFinder;
this.pomDependencyUpdater =
new DefaultPOMDependencyUpdater(
new CodeTFGenerator(positionFinder, dependencyDescriptor), pomFileFinder, pomModifier);
}
MavenProvider(
final PomModifier pomModifier,
final PomFileFinder pomFileFinder,
final DependencyDescriptor dependencyDescriptor) {
this(
pomModifier,
pomFileFinder,
dependencyDescriptor,
new DefaultArtifactInjectionPositionFinder());
}
MavenProvider(final PomModifier pomModifier) {
this(
pomModifier,
new DefaultPomFileFinder(),
DependencyDescriptor.createMarkdownDescriptor(),
new DefaultArtifactInjectionPositionFinder());
}
public MavenProvider() {
this(
new DefaultPomModifier(),
new DefaultPomFileFinder(),
DependencyDescriptor.createMarkdownDescriptor(),
new DefaultArtifactInjectionPositionFinder());
}
@VisibleForTesting
static class DefaultPomFileFinder implements PomFileFinder {
@Override
public Optional<Path> findForFile(final Path projectDir, final Path file) throws IOException {
Path parent = file.getParent();
while (parent != null && !Files.isSameFile(projectDir.getParent(), parent)) {
Path pomPath = parent.resolve("pom.xml");
if (Files.exists(pomPath)) {
return Optional.of(pomPath);
}
parent = parent.getParent();
}
return Optional.empty();
}
}
@Override
public DependencyUpdateResult updateDependencies(
final Path projectDir, final Path file, final List<DependencyGAV> dependencies) {
try {
String dependenciesStr =
dependencies.stream().map(DependencyGAV::toString).collect(Collectors.joining(","));
LOG.trace("Updating dependencies for {} in {}: {}", file, projectDir, dependenciesStr);
final DependencyUpdateResult dependencyUpdateResult =
pomDependencyUpdater.execute(projectDir, file, dependencies);
LOG.trace("Dependency update result: {}", dependencyUpdateResult);
return dependencyUpdateResult;
} catch (Exception e) {
throw new DependencyUpdateException("Failure when updating dependencies", e);
}
}
@Override
public Collection<DependencyGAV> getAllDependencies(final Path projectDir, final Path file) {
try {
final Optional<Path> maybePomFile = pomFileFinder.findForFile(projectDir, file);
if (maybePomFile.isEmpty()) {
LOG.trace("Pom file was empty for {}", file);
return Collections.emptyList();
}
final Path pomFile = maybePomFile.get();
final POMOperator pomOperator = new POMOperator(pomFile, projectDir);
return pomOperator.getAllFoundDependencies();
} catch (Exception e) {
LOG.warn("Not all Maven dependencies could be found", e);
}
return Collections.emptyList();
}
private static final Logger LOG = LoggerFactory.getLogger(MavenProvider.class);
}