org.apache.maven.plugin.PluginParameterExpressionEvaluator Java Examples
The following examples show how to use
org.apache.maven.plugin.PluginParameterExpressionEvaluator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReleaseUtil.java From unleash-maven-plugin with Eclipse Public License 1.0 | 5 votes |
/** * Calculates an SCM tag name based on a pattern. This pattern can include every parameter reference that can be * resolved by <a href= * "https://maven.apache.org/ref/3.3.9/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">PluginParameterExpressionEvaluator</a>. * * @param pattern the pattern for the tag name which may contain variables listed above. * @param project the Maven project to be used for version calculation during parameter resolution. * @param evaluator the Maven plugin parameter expression evaluator used to evaluate expressions containing parameter * references. * @return the name of the tag derived from the pattern. */ public static String getTagName(String pattern, MavenProject project, PluginParameterExpressionEvaluator evaluator) { Preconditions.checkArgument(pattern != null, "Need a tag name pattern to calculate the tag name."); Preconditions.checkArgument(evaluator != null, "Need an expression evaluator to calculate the tag name."); try { StringBuilder sb = new StringBuilder(pattern); int start = -1; while ((start = sb.indexOf("@{")) > -1) { int end = sb.indexOf("}"); String var = sb.substring(start + 2, end); String resolved; // the parameter project.version gets a special treatment and will not be resolved by the evaluator but gets the // release version instead if (Objects.equal("project.version", var)) { resolved = MavenVersionUtil.calculateReleaseVersion(project.getVersion()); } else { String expression = "${" + var + "}"; resolved = evaluator.evaluate(expression).toString(); } sb.replace(start, end + 1, resolved); } return sb.toString(); } catch (ExpressionEvaluationException e) { throw new RuntimeException("Could not resolve expressions in pattern: " + pattern, e); } }
Example #2
Source File: AbstractUnleashMojo.java From unleash-maven-plugin with Eclipse Public License 1.0 | 4 votes |
@MojoProduces private PluginParameterExpressionEvaluator getExpressionEvaluator() { return new PluginParameterExpressionEvaluator(this.session, this.mojoExecution); }
Example #3
Source File: BuildMojo.java From docker-maven-plugin with Apache License 2.0 | 4 votes |
@Override protected void execute(final DockerClient docker) throws MojoExecutionException, GitAPIException, IOException, DockerException, InterruptedException { if (weShouldSkipDockerBuild()) { getLog().info("Skipping docker build"); return; } // Put the list of exposed ports into a TreeSet which will remove duplicates and keep them // in a sorted order. This is useful when we merge with ports defined in the profile. exposesSet = Sets.newTreeSet(exposes); if (runs != null) { runList = Lists.newArrayList(runs); } expressionEvaluator = new PluginParameterExpressionEvaluator(session, execution); final Git git = new Git(); final String commitId = git.isRepository() ? git.getCommitId() : null; if (commitId == null) { final String errorMessage = "Not a git repository, cannot get commit ID. Make sure git repository is initialized."; if (useGitCommitId || ((imageName != null) && imageName.contains("${gitShortCommitId}"))) { throw new MojoExecutionException(errorMessage); } else { getLog().debug(errorMessage); } } else { // Put the git commit id in the project properties. Image names may contain // ${gitShortCommitId} in which case we want to fill in the actual value using the // expression evaluator. We will do that once here for image names loaded from the pom, // and again in the loadProfile method when we load values from the profile. mavenProject.getProperties().put("gitShortCommitId", commitId); if (imageName != null) { imageName = expand(imageName); } if (baseImage != null) { baseImage = expand(baseImage); } } loadProfile(); validateParameters(); final String[] repoTag = parseImageName(imageName); final String repo = repoTag[0]; final String tag = repoTag[1]; if (useGitCommitId) { if (tag != null) { getLog().warn("Ignoring useGitCommitId flag because tag is explicitly set in image name "); } else if (commitId == null) { throw new MojoExecutionException( "Cannot tag with git commit ID because directory not a git repo"); } else { imageName = repo + ":" + commitId; } } mavenProject.getProperties().put("imageName", imageName); final String destination = getDestination(); if (dockerDirectory == null) { final List<String> copiedPaths = copyResources(destination); createDockerFile(destination, copiedPaths); } else { final Resource resource = new Resource(); resource.setDirectory(dockerDirectory); resources.add(resource); copyResources(destination); } buildImage(docker, destination, buildParams()); tagImage(docker, forceTags); final DockerBuildInformation buildInfo = new DockerBuildInformation(imageName, getLog()); if ("docker".equals(mavenProject.getPackaging())) { final File imageArtifact = createImageArtifact(mavenProject.getArtifact(), buildInfo); mavenProject.getArtifact().setFile(imageArtifact); } // Push specific tags specified in pom rather than all images if (pushImageTag) { pushImageTag(docker, imageName, imageTags, getLog(), isSkipDockerPush()); } if (pushImage) { pushImage(docker, imageName, imageTags, getLog(), buildInfo, getRetryPushCount(), getRetryPushTimeout(), isSkipDockerPush()); } if (saveImageToTarArchive != null) { saveImage(docker, imageName, Paths.get(saveImageToTarArchive), getLog()); } // Write image info file writeImageInfoFile(buildInfo, tagInfoFile); }