简体   繁体   中英

Javac to compile a full project with Selenium for Jenkins

Background: I am using jUnit to run Selenium tests. I am trying to set my tests to run with Jenkins nightly. I am able to pull my test/source files nightly and I can build and install my product. I am trying to build the java test files using the "Execute Shell" option in Jenkins on a linux vm.

I have a Java project that consists of a shared Utility project, selenium jars, and my source and test files. I can run the selenium tests in NetBeans without a problem.

The layout of the code is something like:

Utilities project:

src
-> utilities
    -> UtilityFunctions.java
    -> Logger.java

My project

misc
-> XmlConfigFileForTest.xml
-> PasswordFileForTest.txt    
src
-> objects
    -> User.java
    -> ConfigFile.java
test
-> coreTest
    -> coreTest1.java
    -> coreTest2.java
-> setupTest
    -> setupTest1.java
    -> setupTest2.java
-> testSuites
    -> runAllSetupTests.java

I cannot figure out how to run javac such that it will generate a jar that I can use to run my tests.

What I have tried:

Using How to set environment variables for javac to be able to find imported packages? I was able to include my selenium jars.

Once my code is compiled, I believe I can run the tests like How to run JUnit test cases from the command line .

My latest javac is:

javac -classpath selenium-java.jar:selenium-server-standalone.jar -sourcepath code -d build/classes code/test/testSuites/runAllSetupTests.java

This is currently failing because it cannot find the package 'objects'. My very limited understanding is that attempting to javac a file should cause it to pull in all included files.

Question 1: is there a way to just point javac (or another standard program) at a structured project like this and just get the jar out of it? Question 2: is there a better way to do this? I am doing this strictly to run my selenium tests nightly with jenkins. I am not married to the idea of using javac to compile it.

Edit:

Currently trying to use Ant instead of javac directly. Added tag for additional input.

One thing I can think of is that you could continue to use javac to do this but instead of adding the .jar dependencies like selenium jars etc in the command line you can have a .sh file which will set the environment variable say:

CLASSPATH=:./Number1.jar:./Number2.jar:./Number3.jar ...
export CLASSPATH

Add all the dependent jars in following way and just call this .sh file before running any javac command.

say,

setpath.sh 
javac -classpath <mention only .class required from the project to commplie this file> JavaFile1.java
javac -classpath <mention only .class required from the project to commplie this file> JavaFile2.java
javac -classpath <mention only .class required from the project to commplie this file> JavaFile3.java ...
.
.

javac -classpath <mention only .class required from the project to commplie this file> MainJavaFile.java

Java -classpath <all class file required> MainJavaFile

This works fine for me in my project and there should not be any need for converting to executable jar. Just need to figure out the dependent .class files.

Hope this helps!

Without going into a lot of project specific details, I researched and used ant to compile my project. In the end, it was not required to go all the way to a JAR. Ant made making a jar easy enough that it was done as well. The jar created was not made executable because the projects existed to run selenium, and thus there is no real "main" to execute.

I did not use Warman's answer because I can assume that the project will grow and I did not want to have a step in the development process to update the build for testing with each new file.

I imported the two projects into IntelliJ as a single project, and restructured the resulting code so that I could mark the correct directories as code root and test root. I also added the selenium server and java jars to to be part of the project (as opposed to a library that existed elsewhere on my box) so that IntelliJ would be able to use that when it compiled.

I then used IntelliJ's Generate Ant Build to create a default script. This was tweaked over the course of a couple days to exclude/include more specific files that needed to be in place and to use the Jenkins slave's environment.

.....

To anyone finding this question in the future: research Ant and create the script by hand. The single resulting ant script to combine two projects, compile, jar, and run the junit tests was ~250 lines and that could be reduced by someone who actually understands ant.

Good luck.

I am working on the same issue :)

This procedure worked for me: Create a new main method, create the jar file direclty through eclipse and when you have your file you can schedule it via cron job for instance.

This works for sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM