private String buildnumberFromJS(BuildNumber bn) throws ScriptException { ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("JavaScript"); jsEngine.put("tag", bn.getTag()); jsEngine.put("branch", bn.getBranch()); jsEngine.put("revision", bn.getRevision()); jsEngine.put("parent", bn.getParent()); jsEngine.put("shortRevision", bn.getShortRevision()); jsEngine.put("commitsCount", bn.getCommitsCount()); jsEngine.put("commitDate", bn.getCommitDate()); jsEngine.put("describe", bn.getDescribe()); Object res = jsEngine.eval(javaScriptBuildnumberCallback); if (null == res) throw new IllegalStateException("JS buildnumber callback returns null"); return res.toString(); }
/** * Extracts buildnumber fields from git repository and publishes them as maven properties. * Executes only once per build. Return default (unknown) buildnumber fields on error. * * @throws MojoExecutionException * @throws MojoFailureException */ @Override public void execute() throws MojoExecutionException, MojoFailureException { Properties props = project.getProperties(); try { // executes only once per build // http://www.sonatype.com/people/2009/05/how-to-make-a-plugin-run-once-during-a-build/ if (executionRootDirectory.equals(baseDirectory) || !runOnlyAtExecutionRoot) { // build started from this projects root BuildNumber bn = BuildNumberExtractor.extract(repositoryDirectory); props.setProperty(revisionProperty, bn.getRevision()); props.setProperty(shortRevisionProperty, bn.getShortRevision()); props.setProperty(branchProperty, bn.getBranch()); props.setProperty(tagProperty, bn.getTag()); props.setProperty(parentProperty, bn.getParent()); props.setProperty(commitsCountProperty, bn.getCommitsCountAsString()); props.setProperty(commitDateProperty, bn.getCommitDate()); // create composite buildnumber String composite = createBuildnumber(bn); props.setProperty(buildnumberProperty, composite); getLog() .info( String.format( "Git info extracted, revision: '%s', branch: '%s', tag: '%s', commitsCount: '%d', commitDate: '%s', buildNumber: '%s', describe: '%s", bn.getShortRevision(), bn.getBranch(), bn.getTag(), bn.getCommitsCount(), bn.getCommitDate(), composite, bn.getDescribe())); } else if ("pom".equals(parentProject.getPackaging())) { // build started from parent, we are in subproject, lets provide parent properties to our // project Properties parentProps = parentProject.getProperties(); String revision = parentProps.getProperty(revisionProperty); if (null == revision) { // we are in subproject, but parent project wasn't build this time, // maybe build is running from parent with custom module list - 'pl' argument getLog() .info("Cannot extract Git info, maybe custom build with 'pl' argument is running"); fillPropsUnknown(props); return; } props.setProperty(revisionProperty, revision); props.setProperty(shortRevisionProperty, parentProps.getProperty(shortRevisionProperty)); props.setProperty(branchProperty, parentProps.getProperty(branchProperty)); props.setProperty(tagProperty, parentProps.getProperty(tagProperty)); props.setProperty(parentProperty, parentProps.getProperty(parentProperty)); props.setProperty(commitsCountProperty, parentProps.getProperty(commitsCountProperty)); props.setProperty(buildnumberProperty, parentProps.getProperty(buildnumberProperty)); props.setProperty(commitDateProperty, parentProps.getProperty(commitDateProperty)); props.setProperty(describeProperty, parentProps.getProperty(describeProperty)); } else { // should not happen getLog() .warn( "Cannot extract JGit version: something wrong with build process, we're not in parent, not in subproject!"); fillPropsUnknown(props); } } catch (Exception e) { getLog().error(e); fillPropsUnknown(props); } }