ch.sourcepond.maven.release.pom.UpdateParent.java Source code

Java tutorial

Introduction

Here is the source code for ch.sourcepond.maven.release.pom.UpdateParent.java

Source

/*Copyright (C) 2016 Roland Hauser, <sourcepond@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package ch.sourcepond.maven.release.pom;

import static java.lang.String.format;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.model.Model;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import ch.sourcepond.maven.release.reactor.UnresolvedSnapshotDependencyException;

/**
 * @author rolandhauser
 *
 */

@Named("UpdateParent")
@Singleton
final class UpdateParent extends Command {
    static final String ERROR_FORMAT = "The parent of %s is %s %s";

    @Inject
    UpdateParent(final Log pLog) {
        super(pLog);
    }

    @Override
    public void alterModel(final Context updateContext) {
        final MavenProject project = updateContext.getProject();
        final Model model = updateContext.getModel();
        final MavenProject parent = project.getParent();

        if (parent != null && isSnapshot(parent.getVersion())) {
            try {
                final String versionToDependOn = updateContext.getVersionToDependOn(parent.getGroupId(),
                        parent.getArtifactId());
                model.getParent().setVersion(versionToDependOn);
                log.debug(format(" Parent %s rewritten to version %s", parent.getArtifactId(), versionToDependOn));
            } catch (final UnresolvedSnapshotDependencyException e) {
                updateContext.addError(ERROR_FORMAT, project.getArtifactId(), e.artifactId, parent.getVersion());
            }
        }
    }

    @Override
    protected Integer priority() {
        return 2;
    }
}