Jenkins Unable to Detect the Ant - jenkins

I have installed jenkins in windows machine, and i configured environment variable as well.
When i am checking ant -version in cmd i can able to get the response from the terminal "Apache Ant version 1.7.1 compiled on June 27 2008".
Jenkins Configuration
Ant plugin installed.
Ant home configured in jenkins
Ant config in jenkins
I am checking ant -version in pipeline script, but i am getting build failed in jenkins with following error message "ant' is not recognized as an internal or external command"
stage('studio'){
steps {
bat 'ant -version'
}
}
Can you please someone help on this issue.

You are mssing some details in the construction .. bat does not know about ant. See How to invoke Ant in a Jenkins Groovy Pipeline
def antVersion = 'Ant1.9.1'
withEnv( ["ANT_HOME=${tool antVersion}"] ) {
bat '%ANT_HOME%/bin/ant.bat target1 target2'`
}
Also, don't name it ANT_HOME, but something relevant like ant-1.7.1

Related

How to build a Pipeline basic with Jenkins

I try to use Pipeline script and I arrive until last step.
The Pipeline script is the following code
node{
stage ('Build') {
git url: 'https://xxx#bitbucket.org/xxx/01-eureka.git'
withMaven(
// Maven installation declared in the Jenkins "Global Tool Configuration"
maven: 'M3',
// Maven settings.xml file defined with the Jenkins Config File Provider Plugin
// Maven settings and global settings can also be defined in Jenkins Global Tools Configuration
mavenSettingsConfig: 'my-maven-settings',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
} // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs reports...
}
}
I have already been configured the maven and Java.
I don't know the error of the last step , I update all code.
I don't exact ENV as your's, but first thing first, you should check the your Jenkins workspace folder Jenkins\workspace\docker-app, to see if the git clone works as expected
If the source code are there, manual run the maven command to see if Maven finds the pom.xml correctly

Converting Jenkins Project to Pipeline and unable to run ant build get 'cmd' is not recognized

Project builds just fine trying to convert this to run as pipeline. Get error - Running batch script
'cmd' is not recognized as an internal or external command,
operable program or batch file.
Used Convert This Job to Pipeline which didn't quite succeed and had other issues (separate issue). Created Jenkins File and copied in and modified the Pipeline script.
I have tried both WithEnv and WithAnt and same results. Additionally found a note out there and put C:\Windows\System32 in the PATH on the Agent and still get the error. So I'm not sure what I'm missing since I have no issues with my original project and should be calling the same tools I set in Global Tools Config
stage ('PowerSuite - Build') {
agent {
node {
label 'jenkinsclient2'
}
}
steps {
withEnv(["JAVA_HOME=${tool 'JDK_1.7.0_09'}", "PATH=${env.JAVA_HOME}/bin"]){
withAnt(installation: 'Ant 1.7.0') {
echo 'Jenkins client 2 running PowerSuite Build'
bat "ant -Xmx1024m -Xms1024m -buildfile C:\\Builds\\Dev\\Powersuite\\build\\project.xml -Dbuild_type=DEV -DdeployDir=\\\\cifs\\Builds\\PowerSuite\\DEVTest -lib .\buildtoolkit clean ear"
}
}
}
}
Have Ant run the build same as the Jenkins Project

Jenkins: Running groovy script with ant commands

I have a groovy script with ant commands on it. The script is successfully run in my local machine but when I tried it with Jenkins the groovy script always fail. Jenkins always return error that "ant can't create task or type p4Change". I already added Apache ant support in the global configuration. How do I configure ant to successfully run the groovy script I have. Any idea. Thanks.
Sample code snippets:
I have execute.groovy file with ant commands
ant=new AntBuilder()
def checkChanges {
ant.p4change(description:"Checking",port:'perforce:1666',user:optional,view:"'${workspace}'...")}
And I created a batch file that will run the execute.groovy file
call groovy execute.groovy project bopolz18 -c bopolz18.Workspace 150718
In my machine this works well but in Jenkins when I execute the batch command it fails giving the error mention above.

Why won't groovy run in Jenkins pipeline?

I am currently trying to run a groovy script from my pipeline as one of my nodes, but I ran into this error:
[CompanyName] Running shell script
+ ./ideainspect.groovy
env: groovy: No such file or directory
Also, I tried installing the plugin for groovy, but for some reason, it won't install. Whenever I refresh the page for tools, the installer goes away. Am I installing groovy wrong? Please help!
Edit: Relevant Data
stage 'Static Analysis'
node {
dir("Android/btMobileApp") {
sh "./ideainspect.groovy"
sh "./gradlew checkstyle lintDebug"
}
}
And the ideainspect.groovy file is an executable with the shebang #!/usr/bin/env groovy, which could be the problem.
Does your jenkins user have groovy on its path? If so ditch the shebang and try
sh 'groovy ideainspect.groovy'
If it's not on the path, you can try
sh '/usr/bin/groovy ideainspect.groovy'
(or wherever groovy is installed)
Or alternatively add it to the path environment variable using Manage Jenkins -> Configure Systems. This may not be appropriate if you have lots of projects using different versions of groovy

Executing groovy scripts on Jenkins' slaves

I'm trying to execute a simple groovy script on Jenkins' slaves using the Groovy plugin.
I'm using the execute groovy script option and not the execute system groovy script option. I'm also using the "install automatically" (install from groovy website) option of the plugin of the latest version(2.4.6) . However, the job fails and I'm getting this:
Building remotely on .... (...) in workspace C:\Jenkins_Slave\workspace\...
Unpacking https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.6.zip to C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6 on ...
[groovy_reset_dead_slave] $ C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6\bin\groovy.bat C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy: 5: unable to resolve class jenkins.model.Jenkins
# line 5, column 1.
import jenkins.model.Jenkins
^
1 error
Build step 'Execute Groovy script' marked build as failure
Finished: FAILURE
How can I overcome this issue and make it work properly?
From the Groovy plugin page:
Groovy Script vs System Groovy Script
The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. It's the basically the same as running the "groovy" command and pass in the script.
The system groovy script, OTOH, runs inside the Jenkins master's JVM. Thus it will have access to all the internal objects of Jenkins, so you can use this to alter the state of Jenkins. It is similar to the Jenkins Script Console functionality.
When not using the system Groovy, you do not have Jenkins object in your classpath. You need to make sure the required classes are accessible from the job you are running, and pass them in the "Class path" field of the "execute groovy script" step.
We can use RemoteDignostics class
The following Script runs on master but uses RemoteDignostics to run groovy on Worker Nodes/slave.
import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins
String agent_name = 'your agent name'
groovy_script = '''
println System.getenv("PATH")
println "uname -a".execute().text
'''.trim()
String result
Jenkins.instance.slaves.find { agent ->
agent.name == agent_name
}.with { agent ->
result = RemotingDiagnostics.executeGroovy(groovy_script, agent.channel)
}
println result

Resources