Example of Travis CI yml file for java

If you've written tests for you code, you can run them with ./mvnw test locally (Linux and macOS)

This workflow can then be translated to Travis CI by creating travis.yml in the project root / same directory as mvnw files.

Below is an example for a Maven build:

arch: amd64

language: java

jdk: 
  - oraclejdk15

cache:
  directories:
  - $HOME/.m2

script:
  - java --version
  - ./mvnw clean install
  - ./mvnw test

Travis CI is NOT a build tool. It is a Continous Integration tool which usually executes the same build command you would do locally, but automatically after every push to GitHub.

It requires a build mechanism being active. Well, that is not totally true, but it requires you to specify a valid command in the script: section that can be executed on the Travis CI host trying to build your code. When the return code of the command is 0, the build is treated as SUCCESS. Otherwise, it is treated as FAILURE.

(This is all really simplified, best would be to read Travis CI documentation, and perhaps some documents about Continous Integration in general).

In short: Set up your project to use Maven or Gradle or your favourite build tool. You should be able to locally execute mvn clean verify (when using Maven). Then, set up your .travis.yml:

language: java
sudo: false
script: mvn clean verify

And commit & push it, together with the pom.xml (when using Maven). Now, Travis CI should work like a charm.