Howto set up classpath for System Groovy Script in Jenkins - jenkins

Documentation for the Groovy Plugin of Jenkins states that
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.
Yet I find that I have a groovy script that I can successfully run in Jenkins Script Console but which does NOT run if entered as a "System Groovy Script" on a build configuration. There are compiler errors. Clearly, the Jenkins Script Console is running with a different classpath than the script in my build. But I can't find information on what the default classpath is when running a script for a build or what the classpath is when running from the Script Console, so I might duplicate that for my script.
Also, the plugin offers a classpath entry field for running the script as a file but that option does not exist for entering the script as text.
I can't get my script to work either way.
What am I missing?

I think the answer is that the Script Console auto-imports the whole Jenkins library. That is not the case with the System Groovy Script. So what worked for me was to run the script, and for every compiler error about an unknown class, add an import statement for that class. I learned what packages they were from by looking at Javadocs.
Automating this would be a nice improvement to the plugin.

May be use the grab dependency management to resolve the library to add

Related

How to find out the the definition of gradle executed task in Jenkins

I am new to SCM tool like Jenkins so I may not actually ask the right question.
I find out there is a specific Gradle executed task failed when check the logs of Jenkins.
However, I cannot find out where the Gradle executed task is defined and what is the content of it.
I look up the [CONFIGURE] of Jenkins and then in the [BUILD] section, there is a section called [Invoked Gradle script] and I could see the name (lets call it X here) of executed Gradle task I am looking for.
But I cannot find where X is really defined and what command it actually run, Does anyone has any suggestion? Thanks.
You can try using gradle in verbose mode
gradle -S build
-S, --full-stacktrace
Print out the full (very verbose) stacktrace for any exceptions. See also logging options.
or try using debug mode
-d, --debug
Log in debug mode (includes normal stacktrace).
https://docs.gradle.org/current/userguide/command_line_interface.html
Gradle provides a Domain Specific Language (DSL), for describing builds. This uses the Groovy language to make it easier to describe a build. Each build script of Gradle is encoded using UTF-8, saved offline and named as build.gradle.
By default, Gradle command will invoke Gradle script from build.gradle file.
You can run a Gradle build using the Gradle command. This command looks for a file called build.gradle.
More details here.

Configure Jenkins to run Visual Studio bat file before Maven job

I have a Maven module that utilizes the NAR plugin to build some JNI libraries, and in Jenkins I have configured a Maven project to build this module. In order for these libs to be built, the Visual Studio bat file that sets up the path and other environment variables must be run. I have tried several different ways to get this bat file to execute before the Maven commands are called, and none of them have worked correctly. I know that Jenkins isolates all of the build process steps, so it can be difficult to get the environment set up, but I'm hoping someone has solved this particular issue. Here's what I've tried:
calling the bat file as a pre build step.
Using the EnvInject plugin to call the bat file, both as a pre build step and as a pre job step.
Setting the environment variables directly without calling the bat file using EnvInject.
calling the bat file from the mvn.bat file (this failed because it appears Jenkins will call Maven directly, without using the bat file).
As a workaround, I'm using the Freeform project type and setting the build steps to
call the bat file.
Directly call maven with appropriate parameters.
This works, but it's not as nice as using the Maven project type, ex. a failed unit test will fail the entire build instead of just sending a warning. Is there a way to configure this as a Maven project?

how to scan test source code with the SonarQube Jenkins plugin?

I'm trying to make a Jenkins job that only scans the test source files, so everything under /src/test/java (using Maven). I use the SonarQube Jenkins post-action for this.
When we used to configure Sonar in the pom file directly we could do this in a profile:
<sonar.sources>/src/test/java</sonar.sources>
<sonar.tests/>
That worked fine.
But in the Jenkins job I have to specify these as 'Additional properties' and I can't seem to specify an emtpy sonar.tests element. I tried -Dsonar.tests, -Dsonar.tests=,-Dsonar.tests="", nothing works. When this element is not empty Sonar will attempt to scan the test files twice and crash.
The post-build step is specifically and explicitly a Maven operation. Your problem comes from trying to use Maven to do something un-Mavenish; i.e. ignore the convention that files in the tests directory should be treated as tests.
Since you want to scan your tests as code, your best bet is to use the build step (which uses SonarQube Scanner) and set your scanner properties manually. That will make it easy to set your sources directory and to omit the tests directory.

How to override user.home property of ant from Jenkins server?

Hi I need some help in overriding the user.home property of ant from Jenkins. I created a simple job on jenkins that runs a build script on a slave machine of the master. One of the tasks of the scripts is to use the user.home property of ant and access certain files. The problem is that when I try to echo this property from the command line of the slave machine, I see C:\Users\Administrator but when I run the exact the same script from the Jenkins server I see C:\ . This difference is causing the build to fail on Jenkins. Can anyone please tell me what is the reason for this difference? Is there any way I can override this?
I tried having a property called user.home in the config file of my job but it doesn't seem to pick it up. Another interesting point is that when I changed the user.home value to some gibberish in the config file and run ant from command line on the slave, it still picks it up as C:\Users\Administrator
I am using ant 1.7.1 and Jenkins 1.598
You likely have jenkins running as a different ID than Administrator. Which is a good thing.
Properties in ant are immutable so you can specify those properties from the ant plugin in jenkins and they'll override any other values. (click advanced under the invoke ant buildstep to see the properties text box).
I'd recommend you not override user.home but rather refactor your ant so there is a config.location property that defaults to user.home but can be overidden to whatever you want from jenkins. It just seems unnatural to ovrride user.home

Jenkins on Windows using something other than ant

I am looking at using Jenkins on Windows.
I currently have an ant script. It works pretty well. Except for instance, when the build breaks because of a syntax error, I can not see the error in the Jenkins console log.
So I am thinking may be ant is not the best tool for use on Windows.
What do most of you use for Jenkins on Windows?
If it is ant, how do you send the build output, VStudio, to the Jenkins console?
Thanks
Primarily I use Jenkins with maven projects, although you should see the ANT output in the online logs regardless.
What are you building? Are you building Java projects? Ant is what you use. If you are bulding a C project, you should use Make. If you're building a VisualStudio project, you should use msbuild. You use the build tool for your project. Jenkins will execute them without a problem.
Take a look at the build in question. On the left side of the screen, there's a Console output item. Click on that. Is there any output. No matter what tool you use, Jenkins captures the STDOUT and STDERR in that console output. If nothing else, you should see the exact commands Jenkins is executing to checkout and to build your project. Try executing those commands.
Still, you didn't give us much to go on. No idea what you're building or what you're doing with Ant. You didn't state any error, the console output, or even what the Jenkins error log is stating.
Jenkins does two things:
It watches your repository for changes.
Once it detects the changes, it executes the very commands you'd execute to build the project.
Jenkins doesn't care whether you use Ant, Maven, Make, or simply do a del /s/q. Jenkins will simply execute the commands you tell it to execute.
Addendum
It is c, c++, Java and InstallShield. I use ant to do file copy and move, call msdev.exe project. Some Javac calls, InstallShield command line builds..
Jenkins can execute multiple step builds in a single job. After you specify the build step, you can press the Add button to add another build step. There's no reason that all the build steps even have to be of the same type. Just select a "Freestyle" build, and use the right build tools for the job.
There's an optional MSBUILD plugin in Jenkins that should do your MS Build. This should give you the complete output from MSBuild, so you can see any errors.
After you do your MSBuild step, you can create a second build step to run an Ant task to build your InstallShield. After that, you could run another build step to do the copying you need either on the command line or through something like Ant (or Nant.
Whatever the output of the various tools is the output you'll get in the build console.
dev.cmd shows the output and I continue to use ant.

Resources