use of org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef in project pom-manipulation-ext by release-engineering.
the class ListingBlacklistMapper method readValue.
@Override
public Object readValue(String s) {
List<ProjectVersionRef> result = new ArrayList<>();
if (s.length() == 0) {
errorString = "No content to read.";
return result;
} else if (s.startsWith("<")) {
// Read an HTML string.
String stripped = s.replaceFirst(".*</h1>\n", "").replaceFirst("\n</body></html>", "");
logger.debug("Read HTML string '{}' rather than a JSON stream; stripping message to {}", s, stripped);
errorString = stripped;
return result;
} else if (s.startsWith("{\\\"message\\\":") || s.startsWith("{\"message\":")) {
String endStripped = s.replace("\\\"}", "").replace("\"}", "");
errorString = endStripped.substring(endStripped.lastIndexOf("\"") + 1);
logger.debug("Read message string {}, processed to {} ", s, errorString);
return result;
}
List<Map<String, Object>> responseBody;
try {
responseBody = objectMapper.readValue(s, List.class);
} catch (IOException e) {
logger.error("Failed to decode map when reading string {}", s);
throw new RestException("Failed to read list-of-maps response from version server: " + e.getMessage(), e);
}
for (Map<String, Object> gav : responseBody) {
String groupId = (String) gav.get("groupId");
String artifactId = (String) gav.get("artifactId");
String version = (String) gav.get("version");
ProjectVersionRef project = new SimpleProjectVersionRef(groupId, artifactId, version);
result.add(project);
}
return result;
}
use of org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef in project pom-manipulation-ext by release-engineering.
the class HttpHeaderHeaderTest method testVerifyContentHeaderMessageNoEscape.
@Test
public void testVerifyContentHeaderMessageNoEscape() {
testResponseStart = "{\"message\":\"";
testResponseEnd = "\"}";
List<ProjectVersionRef> gavs = new ArrayList<ProjectVersionRef>() {
{
add(new SimpleProjectVersionRef("com.example", "example", "1.0"));
}
};
try {
versionTranslator.translateVersions(gavs);
fail("Failed to throw RestException.");
} catch (RestException ex) {
assertTrue(systemOutRule.getLog().contains("message"));
}
}
use of org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef in project pom-manipulation-ext by release-engineering.
the class RESTParametersTest method testVerifyNoGroup.
@Test
public void testVerifyNoGroup() {
this.versionTranslator = new DefaultTranslator(mockServer.getUrl(), RestProtocol.PNC12, 0, Translator.CHUNK_SPLIT_COUNT, "", "");
List<ProjectVersionRef> gavs = new ArrayList<ProjectVersionRef>() {
{
add(new SimpleProjectVersionRef("com.example", "example", "1.0"));
}
};
versionTranslator.translateVersions(gavs);
assertTrue(gavSchema.repositoryGroup == null);
}
use of org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef in project pom-manipulation-ext by release-engineering.
the class ProjectVersioningManipulator method applyVersioningChanges.
/**
* Apply any project versioning changes applicable for the given {@link Model}, using accumulated version-change information stored in the
* {@link VersioningState} instance, and also produced during the initial {@link Manipulator#applyChanges(List)} invocation.
*
* These changes include the main POM version, but may also include the parent declaration and dependencies, if they reference other POMs in the
* current build.
*
* If the project is modified, then it is marked as changed in the {@link ManipulationSession}, which triggers the associated POM to be rewritten.
*
* @param project Project undergoing modification.
* @param state the VersioningState
* @return whether any changes have been applied.
* @throws ManipulationException if an error occurs.
*/
// TODO: Loooong method
protected boolean applyVersioningChanges(final Project project, final VersioningState state) throws ManipulationException {
if (!state.hasVersionsByGAVMap()) {
return false;
}
final Model model = project.getModel();
if (model == null) {
return false;
}
String g = model.getGroupId();
String v = model.getVersion();
final Parent parent = model.getParent();
// If the groupId or version is null, it means they must be taken from the parent config
if (g == null && parent != null) {
g = parent.getGroupId();
}
if (v == null && parent != null) {
v = parent.getVersion();
}
boolean changed = false;
Map<ProjectVersionRef, String> versionsByGAV = state.getVersionsByGAVMap();
// If the parent version is not defined, it will be taken automatically from the project version
if (parent != null && parent.getVersion() != null) {
final ProjectVersionRef parentGAV = new SimpleProjectVersionRef(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
if (versionsByGAV.containsKey(parentGAV)) {
final String newVersion = versionsByGAV.get(parentGAV);
logger.debug("Changed parent version to: " + newVersion + " in " + parent);
if (parentGAV.getVersionString().startsWith("${")) {
if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(parentGAV.getVersionString()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
logger.error("Unable to find property {} to update with version {}", parentGAV.getVersionString(), newVersion);
}
} else {
parent.setVersion(newVersion);
}
changed = true;
}
}
ProjectVersionRef gav = new SimpleProjectVersionRef(g, model.getArtifactId(), v);
if (model.getVersion() != null) {
final String newVersion = versionsByGAV.get(gav);
logger.info("Looking for new version: " + gav + " (found: " + newVersion + ")");
if (newVersion != null && model.getVersion() != null) {
if (gav.getVersionString().startsWith("${")) {
if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(gav.getVersionString()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
logger.error("Unable to find property {} to update with version {}", gav.getVersionString(), newVersion);
}
} else {
model.setVersion(newVersion);
}
logger.info("Changed main version in " + gav(model));
changed = true;
}
} else // force inject the new version.
if (changed == false && model.getVersion() == null && project.isInheritanceRoot()) {
final String newVersion = versionsByGAV.get(gav);
logger.info("Looking to force inject new version for : " + gav + " (found: " + newVersion + ")");
if (newVersion != null) {
model.setVersion(newVersion);
changed = true;
}
}
final Set<ModelBase> bases = new HashSet<>();
bases.add(model);
bases.addAll(ProfileUtils.getProfiles(session, model));
final PropertyInterpolator pi = new PropertyInterpolator(model.getProperties(), project);
for (final ModelBase base : bases) {
final DependencyManagement dm = base.getDependencyManagement();
if (dm != null && dm.getDependencies() != null) {
for (final Dependency d : dm.getDependencies()) {
if (isEmpty(pi.interp(d.getVersion()))) {
logger.trace("Skipping dependency " + d + " as empty version.");
continue;
}
try {
gav = new SimpleProjectVersionRef(pi.interp(d.getGroupId()), pi.interp(d.getArtifactId()), pi.interp(d.getVersion()));
final String newVersion = versionsByGAV.get(gav);
if (newVersion != null) {
logger.debug("Examining dependency (from depMgmt) {} to change version to {} ", d, newVersion);
if (d.getVersion().startsWith("${")) {
if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(d.getVersion()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
logger.error("Unable to find property {} to update with version {}", d.getVersion(), newVersion);
}
} else {
d.setVersion(newVersion);
logger.info("Changed managed: " + d + " in " + base + " to " + newVersion + " from " + gav.getVersionString());
}
changed = true;
}
} catch (InvalidRefException ire) {
logger.debug("Unable to change version for dependency {} due to {} ", d.toString(), ire);
}
}
}
if (base.getDependencies() != null) {
for (final Dependency d : base.getDependencies()) {
try {
if (isEmpty(pi.interp(d.getVersion()))) {
logger.trace("Skipping dependency " + d + " as empty version.");
continue;
}
gav = new SimpleProjectVersionRef(pi.interp(d.getGroupId()), pi.interp(d.getArtifactId()), pi.interp(d.getVersion()));
final String newVersion = versionsByGAV.get(gav);
if (newVersion != null && d.getVersion() != null) {
logger.debug("Examining dependency {} to change version to {} ", d, newVersion);
if (d.getVersion().startsWith("${")) {
if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(d.getVersion()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
logger.error("Unable to find property {} to update with version {}", d.getVersion(), newVersion);
}
} else {
d.setVersion(newVersion);
logger.info("Changed: " + d + " in " + base + " to " + newVersion + " from " + gav.getVersionString());
}
changed = true;
}
} catch (InvalidRefException ire) {
logger.debug("Unable to change version for dependency {} due to {} ", d.toString(), ire);
}
}
}
}
return changed;
}
use of org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef in project pom-manipulation-ext by release-engineering.
the class RESTCollector method collect.
/**
* Prescans the Project to build up a list of Project GAs and also the various Dependencies.
*/
private void collect(final List<Project> projects) throws ManipulationException {
final RESTState state = session.getState(RESTState.class);
final VersioningState vs = session.getState(VersioningState.class);
final DependencyState ds = session.getState(DependencyState.class);
final PluginState ps = session.getState(PluginState.class);
if (!session.isEnabled() || !state.isEnabled()) {
logger.debug(getClass().getSimpleName() + ": Nothing to do!");
return;
}
final ArrayList<ProjectVersionRef> restParam = new ArrayList<>();
final ArrayList<ProjectVersionRef> newProjectKeys = new ArrayList<>();
final String override = vs.getOverride();
for (final Project project : projects) {
if (isEmpty(override)) {
// TODO: Check this : For the rest API I think we need to check every project GA not just inheritance root.
// Strip SNAPSHOT from the version for matching. DA will handle OSGi conversion.
ProjectVersionRef newKey = new SimpleProjectVersionRef(project.getKey());
if (project.getKey().getVersionString().endsWith("-SNAPSHOT")) {
if (!vs.preserveSnapshot()) {
newKey = new SimpleProjectVersionRef(project.getKey().asProjectRef(), project.getKey().getVersionString().substring(0, project.getKey().getVersionString().indexOf("-SNAPSHOT")));
} else {
logger.warn("SNAPSHOT detected for REST call but preserve-snapshots is enabled.");
}
}
newProjectKeys.add(newKey);
} else if (project.isExecutionRoot()) {
// We want to manually override the version ; therefore ignore what is in the project and calculate potential
// matches for that instead.
Project p = projects.get(0);
newProjectKeys.add(new SimpleProjectVersionRef(p.getGroupId(), p.getArtifactId(), override));
}
}
restParam.addAll(newProjectKeys);
// We only recognise dependencyManagement of the form g:a:version-rebuild not g:a:version-rebuild-<numeric>.
for (ProjectVersionRef bom : (ds.getRemoteBOMDepMgmt() == null ? Collections.<ProjectVersionRef>emptyList() : ds.getRemoteBOMDepMgmt())) {
if (!Version.hasBuildNumber(bom.getVersionString()) && bom.getVersionString().contains(PropertiesUtils.getSuffix(session))) {
// Create the dummy PVR to send to DA (which requires a numeric suffix).
ProjectVersionRef newBom = new SimpleProjectVersionRef(bom.asProjectRef(), bom.getVersionString() + "-0");
logger.debug("Adding dependencyManagement BOM {} into REST call.", newBom);
restParam.add(newBom);
}
}
Set<ArtifactRef> localDeps = establishAllDependencies(session, projects, null);
// Need to send that to the rest interface to get a translation.
for (ArtifactRef p : localDeps) {
restParam.add(p.asProjectVersionRef());
}
// Call the REST to populate the result.
logger.debug("Passing {} GAVs following into the REST client api {} ", restParam.size(), restParam);
logger.info("Calling REST client...");
long start = System.nanoTime();
Map<ProjectVersionRef, String> restResult = null;
try {
restResult = state.getVersionTranslator().translateVersions(restParam);
} finally {
printFinishTime(start, (restResult != null));
}
logger.debug("REST Client returned {} ", restResult);
// Process rest result for boms
ListIterator<ProjectVersionRef> iterator = (ds.getRemoteBOMDepMgmt() == null ? Collections.<ProjectVersionRef>emptyList().listIterator() : ds.getRemoteBOMDepMgmt().listIterator());
while (iterator.hasNext()) {
ProjectVersionRef pvr = iterator.next();
// As before, only process the BOMs if they are of the format <rebuild suffix> without a numeric portion.
if (!Version.hasBuildNumber(pvr.getVersionString()) && pvr.getVersionString().contains(PropertiesUtils.getSuffix(session))) {
// Create the dummy PVR to compare with results to...
ProjectVersionRef newBom = new SimpleProjectVersionRef(pvr.asProjectRef(), pvr.getVersionString() + "-0");
if (restResult.keySet().contains(newBom)) {
ProjectVersionRef replacementBOM = new SimpleProjectVersionRef(pvr.asProjectRef(), restResult.get(newBom));
logger.debug("Replacing BOM value of {} with {}.", pvr, replacementBOM);
iterator.remove();
iterator.add(replacementBOM);
}
}
}
vs.setRESTMetadata(parseVersions(session, projects, state, newProjectKeys, restResult));
final Map<ArtifactRef, String> overrides = new HashMap<>();
// Convert the loaded remote ProjectVersionRefs to the original ArtifactRefs
for (ArtifactRef a : localDeps) {
if (restResult.containsKey(a.asProjectVersionRef())) {
overrides.put(a, restResult.get(a.asProjectVersionRef()));
}
}
logger.debug("Setting REST Overrides {} ", overrides);
ds.setRemoteRESTOverrides(overrides);
// Unfortunately as everything is just GAVs we have to send everything to the PluginManipulator as well.
ps.setRemoteRESTOverrides(overrides);
}
Aggregations