List of usage examples for org.apache.maven.toolchain Toolchain findTool
String findTool(String toolName);
From source file:com.github.maven_nar.Javah.java
License:Apache License
private String getJavah() throws MojoExecutionException, MojoFailureException { String javah = null;//from w w w. j av a 2 s . c o m // try toolchain final Toolchain toolchain = getToolchain(); if (toolchain != null) { javah = toolchain.findTool("javah"); } // try java home if (javah == null) { final File javahFile = new File(this.mojo.getJavaHome(this.mojo.getAOL()), "bin"); javah = new File(javahFile, this.name).getAbsolutePath(); } // forget it... if (javah == null) { throw new MojoExecutionException("NAR: Cannot find 'javah' in Toolchain or on JavaHome"); } return javah; }
From source file:com.github.maven_nar.NarIntegrationTestMojo.java
License:Apache License
private SurefireBooter constructSurefireBooter() throws MojoExecutionException, MojoFailureException { final SurefireBooter surefireBooter = new SurefireBooter(); final Artifact surefireArtifact = (Artifact) this.pluginArtifactMap .get("org.apache.maven.surefire:surefire-booter"); if (surefireArtifact == null) { throw new MojoExecutionException("Unable to locate surefire-booter in the list of plugin artifacts"); }//w w w. j av a 2s . c om surefireArtifact.isSnapshot(); // TODO: this is ridiculous, but it fixes // getBaseVersion to be -SNAPSHOT if // needed Artifact junitArtifact; Artifact testNgArtifact; try { addArtifact(surefireBooter, surefireArtifact); junitArtifact = (Artifact) this.projectArtifactMap.get(this.junitArtifactName); // SUREFIRE-378, junit can have an alternate artifact name if (junitArtifact == null && "junit:junit".equals(this.junitArtifactName)) { junitArtifact = (Artifact) this.projectArtifactMap.get("junit:junit-dep"); } // TODO: this is pretty manual, but I'd rather not require the plugin > // dependencies section right now testNgArtifact = (Artifact) this.projectArtifactMap.get(this.testNGArtifactName); if (testNgArtifact != null) { final VersionRange range = VersionRange.createFromVersionSpec("[4.7,)"); if (!range.containsVersion(new DefaultArtifactVersion(testNgArtifact.getVersion()))) { throw new MojoFailureException( "TestNG support requires version 4.7 or above. You have declared version " + testNgArtifact.getVersion()); } convertTestNGParameters(); if (this.testClassesDirectory != null) { this.properties.setProperty("testng.test.classpath", this.testClassesDirectory.getAbsolutePath()); } addArtifact(surefireBooter, testNgArtifact); // The plugin uses a JDK based profile to select the right testng. We // might be explicity using a // different one since its based on the source level, not the JVM. Prune // using the filter. addProvider(surefireBooter, "surefire-testng", surefireArtifact.getBaseVersion(), testNgArtifact); } else if (junitArtifact != null && junitArtifact.getBaseVersion().startsWith("4")) { addProvider(surefireBooter, "surefire-junit4", surefireArtifact.getBaseVersion(), null); } else { // add the JUnit provider as default - it doesn't require JUnit to be // present, // since it supports POJO tests. addProvider(surefireBooter, "surefire-junit", surefireArtifact.getBaseVersion(), null); } } catch (final ArtifactNotFoundException e) { throw new MojoExecutionException( "Unable to locate required surefire provider dependency: " + e.getMessage(), e); } catch (final InvalidVersionSpecificationException e) { throw new MojoExecutionException("Error determining the TestNG version requested: " + e.getMessage(), e); } catch (final ArtifactResolutionException e) { throw new MojoExecutionException("Error to resolving surefire provider dependency: " + e.getMessage(), e); } if (this.suiteXmlFiles != null && this.suiteXmlFiles.length > 0 && this.test == null) { if (testNgArtifact == null) { throw new MojoExecutionException("suiteXmlFiles is configured, but there is no TestNG dependency"); } // TODO: properties should be passed in here too surefireBooter.addTestSuite("org.apache.maven.surefire.testng.TestNGXmlTestSuite", new Object[] { this.suiteXmlFiles, this.testSourceDirectory.getAbsolutePath(), testNgArtifact.getBaseVersion(), testNgArtifact.getClassifier(), this.properties, this.reportsDirectory }); } else { List includeList; List excludeList; if (this.test != null) { // Check to see if we are running a single test. The raw parameter will // come through if it has not been set. // FooTest -> **/FooTest.java includeList = new ArrayList(); excludeList = new ArrayList(); if (this.failIfNoTests == null) { this.failIfNoTests = Boolean.TRUE; } final String[] testRegexes = StringUtils.split(this.test, ","); for (final String testRegexe : testRegexes) { String testRegex = testRegexe; if (testRegex.endsWith(".java")) { testRegex = testRegex.substring(0, testRegex.length() - 5); } // Allow paths delimited by '.' or '/' testRegex = testRegex.replace('.', '/'); includeList.add("**/" + testRegex + ".java"); } } else { includeList = this.includes; excludeList = this.excludes; // defaults here, qdox doesn't like the end javadoc value // Have to wrap in an ArrayList as surefire expects an ArrayList instead // of a List for some reason if (includeList == null || includeList.size() == 0) { includeList = new ArrayList( Arrays.asList(new String[] { "**/Test*.java", "**/*Test.java", "**/*TestCase.java" })); } if (excludeList == null || excludeList.size() == 0) { excludeList = new ArrayList(Arrays.asList(new String[] { "**/*$*" })); } } if (testNgArtifact != null) { surefireBooter.addTestSuite("org.apache.maven.surefire.testng.TestNGDirectoryTestSuite", new Object[] { this.testClassesDirectory, includeList, excludeList, this.testSourceDirectory.getAbsolutePath(), testNgArtifact.getBaseVersion(), testNgArtifact.getClassifier(), this.properties, this.reportsDirectory }); } else { String junitDirectoryTestSuite; if (junitArtifact != null && junitArtifact.getBaseVersion() != null && junitArtifact.getBaseVersion().startsWith("4")) { junitDirectoryTestSuite = "org.apache.maven.surefire.junit4.JUnit4DirectoryTestSuite"; } else { junitDirectoryTestSuite = "org.apache.maven.surefire.junit.JUnitDirectoryTestSuite"; } // fall back to JUnit, which also contains POJO support. Also it can run // classes compiled against JUnit since it has a dependency on JUnit // itself. surefireBooter.addTestSuite(junitDirectoryTestSuite, new Object[] { this.testClassesDirectory, includeList, excludeList }); } } // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- getLog().debug("Test Classpath :"); // Check if we need to add configured classes/test classes directories here. // If they are configured, we should remove the default to avoid conflicts. if (!this.project.getBuild().getOutputDirectory().equals(this.classesDirectory.getAbsolutePath())) { this.classpathElements.remove(this.project.getBuild().getOutputDirectory()); this.classpathElements.add(this.classesDirectory.getAbsolutePath()); } if (!this.project.getBuild().getTestOutputDirectory().equals(this.testClassesDirectory.getAbsolutePath())) { this.classpathElements.remove(this.project.getBuild().getTestOutputDirectory()); this.classpathElements.add(this.testClassesDirectory.getAbsolutePath()); } for (final Iterator i = this.classpathElements.iterator(); i.hasNext();) { final String classpathElement = (String) i.next(); getLog().debug(" " + classpathElement); surefireBooter.addClassPathUrl(classpathElement); } final Toolchain tc = getToolchain(); if (tc != null) { getLog().info("Toolchain in surefire-plugin: " + tc); if (ForkConfiguration.FORK_NEVER.equals(this.forkMode)) { this.forkMode = ForkConfiguration.FORK_ONCE; } if (this.jvm != null) { getLog().warn("Toolchains are ignored, 'executable' parameter is set to " + this.jvm); } else { this.jvm = tc.findTool("java"); // NOI18N } } if (this.additionalClasspathElements != null) { for (final Iterator i = this.additionalClasspathElements.iterator(); i.hasNext();) { final String classpathElement = (String) i.next(); getLog().debug(" " + classpathElement); surefireBooter.addClassPathUrl(classpathElement); } } // ---------------------------------------------------------------------- // Forking // ---------------------------------------------------------------------- final ForkConfiguration fork = new ForkConfiguration(); // DUNS if (this.project.getPackaging().equals("nar") || getNarArtifacts().size() > 0) { this.forkMode = "pertest"; } fork.setForkMode(this.forkMode); processSystemProperties(!fork.isForking()); if (getLog().isDebugEnabled()) { showMap(this.systemProperties, "system property"); } if (fork.isForking()) { this.useSystemClassLoader = this.useSystemClassLoader == null ? Boolean.TRUE : this.useSystemClassLoader; fork.setUseSystemClassLoader(this.useSystemClassLoader.booleanValue()); fork.setUseManifestOnlyJar(this.useManifestOnlyJar); fork.setSystemProperties(this.systemProperties); if ("true".equals(this.debugForkedProcess)) { this.debugForkedProcess = "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"; } fork.setDebugLine(this.debugForkedProcess); if (this.jvm == null || "".equals(this.jvm)) { // use the same JVM as the one used to run Maven (the "java.home" one) this.jvm = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; getLog().debug("Using JVM: " + this.jvm); } fork.setJvmExecutable(this.jvm); if (this.workingDirectory != null) { fork.setWorkingDirectory(this.workingDirectory); } else { fork.setWorkingDirectory(this.basedir); } // BEGINDUNS if (this.argLine == null) { this.argLine = ""; } final StringBuffer javaLibraryPath = new StringBuffer(); if (testJNIModule()) { // Add libraries to java.library.path for testing final File jniLibraryPathEntry = getLayout().getLibDirectory(getTargetDirectory(), getMavenProject().getArtifactId(), getMavenProject().getVersion(), getAOL().toString(), Library.JNI); if (jniLibraryPathEntry.exists()) { getLog().debug("Adding library directory to java.library.path: " + jniLibraryPathEntry); if (javaLibraryPath.length() > 0) { javaLibraryPath.append(File.pathSeparator); } javaLibraryPath.append(jniLibraryPathEntry); } final File sharedLibraryPathEntry = getLayout().getLibDirectory(getTargetDirectory(), getMavenProject().getArtifactId(), getMavenProject().getVersion(), getAOL().toString(), Library.SHARED); if (sharedLibraryPathEntry.exists()) { getLog().debug("Adding library directory to java.library.path: " + sharedLibraryPathEntry); if (javaLibraryPath.length() > 0) { javaLibraryPath.append(File.pathSeparator); } javaLibraryPath.append(sharedLibraryPathEntry); } // add jar file to classpath, as one may want to read a // properties file for artifactId and version final String narFile = "target/" + this.project.getArtifactId() + "-" + this.project.getVersion() + ".jar"; getLog().debug("Adding to surefire test classpath: " + narFile); surefireBooter.addClassPathUrl(narFile); } final List dependencies = getNarArtifacts(); // TODO: get seems heavy, not // sure if we can push this // up to before the fork to // use it multiple times. for (final Iterator i = dependencies.iterator(); i.hasNext();) { final NarArtifact dependency = (NarArtifact) i.next(); // FIXME this should be overridable // NarInfo info = dependency.getNarInfo(); // String binding = info.getBinding(getAOL(), Library.STATIC); // NOTE: fixed to shared, jni final String[] bindings = { Library.SHARED, Library.JNI }; for (final String binding2 : bindings) { final String binding = binding2; if (!binding.equals(Library.STATIC)) { final File depLibPathEntry = getLayout().getLibDirectory(getUnpackDirectory(), dependency.getArtifactId(), dependency.getVersion(), getAOL().toString(), binding); if (depLibPathEntry.exists()) { getLog().debug("Adding dependency directory to java.library.path: " + depLibPathEntry); if (javaLibraryPath.length() > 0) { javaLibraryPath.append(File.pathSeparator); } javaLibraryPath.append(depLibPathEntry); } } } } // add final javalibrary path if (javaLibraryPath.length() > 0) { // NOTE java.library.path only works for the jni lib itself, and // not for its dependent shareables. // NOTE: java.library.path does not work with arguments with // spaces as // SureFireBooter splits the line in parts and then quotes // it wrongly NarUtil.addLibraryPathToEnv(javaLibraryPath.toString(), this.environmentVariables, getOS()); } // necessary to find WinSxS if (getOS().equals(OS.WINDOWS)) { this.environmentVariables.put("SystemRoot", NarUtil.getEnv("SystemRoot", "SystemRoot", "C:\\Windows")); } // ENDDUNS fork.setArgLine(this.argLine); fork.setEnvironmentVariables(this.environmentVariables); if (getLog().isDebugEnabled()) { showMap(this.environmentVariables, "environment variable"); fork.setDebug(true); } if (this.argLine != null) { final List args = Arrays.asList(this.argLine.split(" ")); if (args.contains("-da") || args.contains("-disableassertions")) { this.enableAssertions = false; } } } surefireBooter.setFailIfNoTests(this.failIfNoTests == null ? false : this.failIfNoTests.booleanValue()); surefireBooter.setForkedProcessTimeoutInSeconds(this.forkedProcessTimeoutInSeconds); surefireBooter.setRedirectTestOutputToFile(this.redirectTestOutputToFile); surefireBooter.setForkConfiguration(fork); surefireBooter.setChildDelegation(this.childDelegation); surefireBooter.setEnableAssertions(this.enableAssertions); surefireBooter.setReportsDirectory(this.reportsDirectory); addReporters(surefireBooter, fork.isForking()); return surefireBooter; }
From source file:com.github.paulmoloney.maven.plugins.enforcer.RuleJavaVersionToolchainAware.java
License:Apache License
/** * This particular rule determines if the specified Java compiler version referenced in the toolchains.xml is an appropriate version * @see org.apache.maven.enforcer.rule.api.EnforcerRule&#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper) *///from w w w.j a v a 2 s . co m public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { try { super.init(helper); try { compilerManager = (CompilerManager) helper.getComponent(CompilerManager.class); } catch (ComponentLookupException e) { throw new MojoExecutionException("Unable to retrieve component", e); } if (null == compilerId || "".equals(compilerId)) { compilerId = "javac"; } if (null == compilerArgument || "".equals(compilerArgument)) { compilerArgument = "-version"; } /*try { compilerId = (String) helper.evaluate("maven.compiler.compilerId"); } catch (ExpressionEvaluationException e) { throw new MojoExecutionException ("Unable to determine compiler id", e); }*/ } catch (MojoExecutionException e) { throw new EnforcerRuleException("Error initialising mojo", e); } String java_version; final Log log = helper.getLog(); log.debug("Using compiler id'" + getCompilerId() + "'."); try { getCompilerManager().getCompiler(getCompilerId()); } catch (NoSuchCompilerException e) { throw new EnforcerRuleException("No compiler with id: '" + e.getCompilerId() + "'."); } try { Toolchain tc = findToolChain("jdk", helper, null); if (tc != null) { executable = tc.findTool(getCompilerId()); } } catch (MojoExecutionException e) { throw new EnforcerRuleException("", e); } if (null == executable && isFallBackAllowed()) { executable = findToolExecutable(getCompilerId() + getExecutableExtension(), log, "java.home", new String[] { "../bin", "bin", "../sh" }, new String[] { "JDK_HOME", "JAVA_HOME" }, new String[] { "bin", "sh" }); } if (null == executable || "".equals(executable.trim())) { throw new EnforcerRuleException("No valid executable found, aborting"); } setProcess(process); java_version = runToolAndRetrieveVersion(process, log); String clean_java_version = normalizeJDKVersion(java_version); log.debug("Normalized Java Version: " + clean_java_version); ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion(clean_java_version); log.debug("Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: " + detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion() + " Build: " + detectedJdkVersion.getBuildNumber() + "Qualifier: " + detectedJdkVersion.getQualifier()); log.debug("Rule requires: " + version); enforceVersion(log, "JDK", getVersion(), detectedJdkVersion); }
From source file:com.google.code.tycho.eclipserun.EclipseRunMojo.java
License:Open Source License
LaunchConfiguration createCommandLine(EquinoxInstallation runtime) throws MalformedURLException { EquinoxLaunchConfiguration cli = new EquinoxLaunchConfiguration(runtime); String executable = null;/*from ww w. j a va 2 s . c o m*/ Toolchain tc = getToolchain(); if (tc != null) { getLog().info("Toolchain in tycho-eclipserun-plugin: " + tc); executable = tc.findTool("java"); } cli.setJvmExecutable(executable); cli.setWorkingDirectory(project.getBasedir()); if (argLine != null) { cli.addVMArguments(false, argLine); } addProgramArgs(true, cli, "-install", runtime.getLocation().getAbsolutePath(), "-configuration", new File(work, "configuration").getAbsolutePath()); addProgramArgs(false, cli, appArgLine); if (environmentVariables != null) { cli.addEnvironmentVariables(environmentVariables); } return cli; }
From source file:com.googlecode.mycontainer.maven.plugin.ExecMojo.java
License:Apache License
CommandLine getExecutablePath(Map enviro, File dir) { File execFile = new File(executable); String exec = null;// w w w . j ava2 s .c o m if (execFile.exists()) { getLog().debug("Toolchains are ignored, 'executable' parameter is set to " + executable); exec = execFile.getAbsolutePath(); } else { Toolchain tc = getToolchain(); // if the file doesn't exist & toolchain is null, the exec is // probably in the PATH... // we should probably also test for isFile and canExecute, but the // second one is only // available in SDK 6. if (tc != null) { getLog().info("Toolchain in exec-maven-plugin: " + tc); exec = tc.findTool(executable); } else { if (OS.isFamilyWindows()) { String ex = executable.indexOf(".") < 0 ? executable + ".bat" : executable; File f = new File(dir, ex); if (f.exists()) { exec = ex; } else { // now try to figure the path from PATH, PATHEXT env // vars // if bat file, wrap in cmd /c String path = (String) enviro.get("PATH"); if (path != null) { String[] elems = StringUtils.split(path, File.pathSeparator); for (int i = 0; i < elems.length; i++) { f = new File(new File(elems[i]), ex); if (f.exists()) { exec = ex; break; } } } } } } } if (exec == null) { exec = executable; } CommandLine toRet; if (OS.isFamilyWindows() && exec.toLowerCase(Locale.getDefault()).endsWith(".bat")) { toRet = new CommandLine("cmd"); toRet.addArgument("/c"); toRet.addArgument(exec); } else { toRet = new CommandLine(exec); } return toRet; }
From source file:com.semperos.mojo.jall.AbstractJAllCompilerMojo.java
License:Open Source License
private String getJavaExecutable() throws MojoExecutionException { Toolchain tc = toolchainManager.getToolchainFromBuildContext("jdk", //NOI18N session);//from w w w . j av a2 s .co m if (tc != null) { getLog().info("Toolchain in jall-maven-plugin: " + tc); String foundExecutable = tc.findTool("java"); if (foundExecutable != null) { return foundExecutable; } else { throw new MojoExecutionException("Unable to find 'java' executable for toolchain: " + tc); } } return "java"; }
From source file:com.soebes.maven.plugins.doxygen.AbstractDoxygenMojo.java
License:Apache License
/** * This will check if the given information for the doxygen executable * is enough. If not than we search on the path for doxygen executable. * @return Path to the doxygen executable. *//*ww w. j a v a 2s. c o m*/ private String getExecutablePath() { File execFile = new File(executable); if (execFile.exists()) { getLog().debug("Toolchains are ignored, 'executable' parameter is set to " + executable); return execFile.getAbsolutePath(); } else { Toolchain tc = getToolchain(); // if the file doesn't exist & toolchain is null, the exec is probably in the PATH... // we should probably also test for isFile and canExecute, but the second one is only // available in SDK 6. if (tc != null) { getLog().info("Toolchain in doxygen plugin: " + tc); executable = tc.findTool(executable); } } return executable; }
From source file:com.theoryinpractise.clojure.AbstractClojureCompilerMojo.java
License:Open Source License
private String getJavaExecutable() throws MojoExecutionException { Toolchain tc = toolchainManager.getToolchainFromBuildContext("jdk", //NOI18N session);//from w ww. ja v a 2 s. com if (tc != null) { getLog().info("Toolchain in clojure-maven-plugin: " + tc); String foundExecutable = tc.findTool("java"); if (foundExecutable != null) { return foundExecutable; } else { throw new MojoExecutionException("Unable to find 'java' executable for toolchain: " + tc); } } return "java"; }
From source file:io.gatling.mojo.Fork.java
License:Apache License
private String findJavaExecutable(Toolchain toolchain) { String fromToolchain = toolchain != null ? toolchain.findTool("java") : null; if (fromToolchain != null) { return fromToolchain; } else {/*ww w. j ava 2 s . c om*/ String javaHome; javaHome = System.getProperty("java.home"); if (javaHome == null) { javaHome = System.getenv("JAVA_HOME"); if (javaHome == null) { throw new IllegalStateException( "Couldn't locate java, try setting JAVA_HOME environment variable."); } } return javaHome + File.separator + "bin" + File.separator + "java"; } }
From source file:kr.motd.maven.exec.ExecMojo.java
License:Apache License
CommandLine getExecutablePath(Map<String, String> enviro, File dir) { File execFile = new File(executable); String exec = null;//from w w w . j a v a 2 s. com if (execFile.isFile()) { getLog().debug("Toolchains are ignored, 'executable' parameter is set to " + executable); exec = execFile.getAbsolutePath(); } if (exec == null) { Toolchain tc = getToolchain(); // if the file doesn't exist & toolchain is null, the exec is probably in the PATH... // we should probably also test for isFile and canExecute, but the second one is only // available in SDK 6. if (tc != null) { getLog().info("Toolchain in exec-maven-plugin: " + tc); exec = tc.findTool(executable); } else { if (OS.isFamilyWindows()) { String ex = !executable.contains(".") ? executable + ".bat" : executable; File f = new File(dir, ex); if (f.isFile()) { exec = ex; } if (exec == null) { // now try to figure the path from PATH, PATHEXT env vars // if bat file, wrap in cmd /c String path = enviro.get("PATH"); if (path != null) { String[] elems = StringUtils.split(path, File.pathSeparator); for (String elem : elems) { f = new File(new File(elem), ex); if (f.isFile()) { exec = ex; break; } } } } } } } if (exec == null) { exec = executable; } CommandLine toRet; if (OS.isFamilyWindows() && exec.toLowerCase(Locale.getDefault()).endsWith(".bat")) { toRet = new CommandLine("cmd"); toRet.addArgument("/c"); toRet.addArgument(exec); } else { toRet = new CommandLine(exec); } return toRet; }