Skip to content

Commit

Permalink
Migrating build to Gradle (#55)
Browse files Browse the repository at this point in the history
* Bump version to 1.6.0-SNAPSHOT before we start conversion
* Jlinked application support
* Use two test-servers. One for JDK8 and one for JDK11
* Update certificates
  • Loading branch information
ysb33r authored Oct 1, 2020
1 parent f8f1669 commit c9b0ae5
Show file tree
Hide file tree
Showing 54 changed files with 1,974 additions and 1,382 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
name: Java CI
on: [ push ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: '11'
java-package: jdk
- name: Build with Gradle
run: ./gradlew build -i -s
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
**/*.iml
**/*.iws
**/*.ipr
out/

# Package Files #
*.jar
!gradle/wrapper/*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
**/target

# Gradle
.gradle
build/
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.NEXT
- Migrate from Maven to Gradle for building the project
- Add JLink support

## 1.5.3

Expand Down
100 changes: 92 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,21 @@ $ ./bin/nrjmx -hostname localhost -port 1689 -uriPath "org.opends.server.protoco

If you are having difficulties with `nrjmx` to get data out of your JMX service, we provide a CLI tool (`jmxterm`) to help you [troubleshoot](./TROUBLESHOOT.md).


## Building

nrjmx uses Maven for generating the binaries:
nrjmx uses Gradle for generating the binaries:

```bash
$ mvn package
$ ./gradlew build
```

This creates the `nrjmx.jar` file under the `./bin/` directory. Copy the `bin/nrjmx` and `bin/nrjmx.jar` files to your preferred location. Both files must
be located under the same folder.

It also creates DEB and RPM packages to automatically install nrjmx. If you want to skip the creation of DEB and RPM packages (for example, because your development machine does not provide the required tools), you can disable the `deb` and `rpm` Maven profiles from the command line:
This creates the modularised `nrjmx.jar` file under the `./build/libs` directory as well as a `tar` and `zip` under the `build/distributions` directory. It can also build RPM & DEB packages.

```bash
mvn clean package -P \!deb,\!rpm,\!tarball,\!test
$ ./gradlew buildDeb # Debian package
$ ./gradlew buildRpm # RPM package
$ ./gradlew distTar # Tar ball
$ ./gradlew distZip # ZIP package
```

## Support
Expand All @@ -113,6 +112,91 @@ New Relic hosts and moderates an online forum where customers can interact with

https://discuss.newrelic.com/c/support-products-agents/new-relic-infrastructure

## Using testservers for testing

It builds a test service that introduces some monitoring.

### Build

`./gradlew :test-server-jdk8:build` will generate both a runnable jar file as well as copy the appropriate files from `src/docker` to a location where it can be used to build a container.
Replace `test-server-jdk8` with `test-server-jdk11` to build a runnable jar with JDK11


### Run (with JMX enabled)

The project itself does not run the container.
Instead this happens when `./gradlew :test` is executed.
The containers are built from within testcontainers and then executed.

It uses the following ports:

* `4567`: HTTP REST port
* `7199`: JMX RMI port

If you want to enable SSL, `JAVA_OPTS` should be:

```
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=true
-Dcom.sun.management.jmxremote.ssl.need.client.auth=true
-Dcom.sun.management.jmxremote.registry.ssl=true
-Djavax.net.ssl.keyStore=/serverkeystore
-Djavax.net.ssl.keyStorePassword=serverpass
-Djavax.net.ssl.trustStore=/servertruststore
-Djavax.net.ssl.trustStorePassword=servertrustpass
```

(This is already done with the test code).

## Building the container manually

If you need to build and run the container manually then you can do:

```
./gradlew :test-server-jdk8:install
docker build test-server-jdk8/build/install/test-server-jdk8
```

(Replace with `test-server-jdk11` as appropriate).

### REST API:

In the port `4567`:

* `POST /cat`
* BODY: `{"name":"Isidoro"}` would register in JMX a cat named Isidoro to the registry name

* `PUT /clear`
* Will clear all the cats from JMX

### Example JMX test

```
$ curl -X POST -d '{"name":"Isidoro"}' http://localhost:4567/cat
ok!
$ curl -X POST -d '{"name":"Heathcliff"}' http://localhost:4567/cat
ok!
$ ./nrjmx
test:type=Cat,*
{"test:type\u003dCat,name\u003dIsidoro,attr\u003dName":"Isidoro","test:type\u003dCat,name\u003dHeathcliff,attr\u003dName":"Heathcliff"}
$ curl -X PUT http://localhost:4567/clear
ok!
$ ./nrjmx
test:type=Cat,*
{}
```

### Re-generating keys

```
keytool -genkeypair -dname "cn=server, ou=nrjmx, o=NR, c=US" -keystore serverkeystore -keyalg RSA -alias serverkey -validity 180 -storepass serverpass -keypass serverpass
keytool -exportcert -keystore serverkeystore -alias serverkey -storepass serverpass -file server.cer
keytool -import -v -trustcacerts -alias serverkey -file server.cer -keystore clienttruststore -storepass clienttrustpass -noprompt
keytool -genkeypair -dname "cn=client, ou=test, o=NR, c=US" -keystore clientkeystore -keyalg RSA -alias clientkey -validity 180 -storepass clientkeystore -keypass clientkeystore
keytool -exportcert -keystore clientkeystore -alias clientkey -storepass clientkeystore -file client.cer
keytool -import -v -trustcacerts -alias clientkey -file client.cer -keystore servertruststore -storepass servertrustpass -noprompt
```

## Contributing
We encourage your contributions to improve New Relic JMX fetcher! Keep in mind when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project.
If you have any questions, or to execute our corporate CLA, required if your contribution is on behalf of a company, please drop us an email at [email protected].
Expand Down
40 changes: 0 additions & 40 deletions assembly.xml

This file was deleted.

167 changes: 167 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import org.redline_rpm.header.Architecture.X86_64
import org.redline_rpm.header.Os.LINUX
import fi.linuxbox.gradle.download.Download

plugins {
java
`maven-publish`
id("org.beryx.jlink") version ("2.21.2")
id("org.ysb33r.java.modulehelper") version ("0.9.0")
id("com.github.sherter.google-java-format") version ("0.8")
id("nebula.ospackage") version ("8.4.1")
id("fi.linuxbox.download") version ("0.6")
}

val jmxTermVersion: String by project

allprojects {
repositories {
mavenCentral()
maven {
url = uri("https://repo.maven.apache.org/maven2")
}
}
}

configurations.create("jmxterm")

dependencies {
implementation("commons-cli:commons-cli:1.4")
implementation("com.google.code.gson:gson:2.8.0")
}

extraJavaModules {
module("commons-cli-1.4.jar", "commons.cli", "1.4") {
exports("org.apache.commons.cli")
}
module("gson-2.8.0.jar", "com.google.code.gson", "2.8.0") {
exports("com.google.gson")
}
}

java {
modularity.inferModulePath.set(true)
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

publishing {
publications {
create<MavenPublication>("nrjmx") {
from(components["java"])
}
}
}

application {
mainClass.set("org.newrelic.nrjmx.Application")
mainModule.set("org.newrelic.nrjmx")
}

jlink {
}

tasks.named<JavaCompile>("compileJava") {
options.javaModuleVersion.set(provider({ project.version as String }))
}

tasks.test {
enabled = false
}

tasks.register<Download>("downloadJmxTerm") {
group = "JmxTerm"
description = "Downloads jmxterm"
from ("https://github.com/jiaqi/jmxterm/releases/download/v${jmxTermVersion}/jmxterm-${jmxTermVersion}-uber.jar")
to ("${buildDir}/jmxterm/lib/jmxterm-uber.jar")
}

tasks.register<CreateStartScripts>("jmxtermScripts") {
group = "JmxTerm"
description = "Scripts to wrap jmxterm"
applicationName = "jmxterm"
outputDir = file("${buildDir}/jmxterm/bin")
mainClassName = ""
(unixStartScriptGenerator as TemplateBasedScriptGenerator).template = project.resources.text.fromFile(file("src/jmxterm/jmxterm.template.sh"))
(windowsStartScriptGenerator as TemplateBasedScriptGenerator).template = project.resources.text.fromFile(file("src/jmxterm/jmxterm.template.bat"))
}


tasks.buildDeb {
dependsOn(tasks.jlink)

from("${buildDir}/jmxterm") {
include ("lib/jmxterm-uber.jar")
into("/usr/lib/${project.name}")
}

from("src/deb/usr/bin") {
into("/usr/bin")
include("**")
fileMode = 0x1ED
}
from("${buildDir}/image") {
into("/usr/lib/${project.name}")
}
from("LICENSE") {
into("/usr/share/doc/${project.name}")
}
from("README.md") {
into("/usr/share/doc/${project.name}")
}
}

tasks.buildRpm {
setRelease("1")
setArch(X86_64)
setOs(LINUX)
setVendor("New Relic Infrastructure Team <[email protected]>")
setPackageGroup("Application/System")
setLicense("Apache 2.0")

dependsOn(tasks.jlink)

from("${buildDir}/jmxterm") {
include ("lib/jmxterm-uber.jar")
into("/usr/lib/${project.name}")
}

from("src/rpm/usr/bin") {
into("/usr/bin")
include("**")
fileMode = 0x1ED
}
from("${buildDir}/image") {
into("/usr/lib/${project.name}")
}
from("LICENSE") {
into("/usr/share/doc/${project.name}")
addParentDirs = false
}
from("README.md") {
into("/usr/share/doc/${project.name}")
addParentDirs = false
}
}

tasks.distZip {
dependsOn("downloadJmxTerm","jmxtermScripts")
from("${buildDir}/jmxterm") {
include ("**")
into ("${project.name}-${project.version}")
}
}

tasks.distTar {
dependsOn("downloadJmxTerm","jmxtermScripts")
from("${buildDir}/jmxterm") {
include ("**")
into ("${project.name}-${project.version}")
}
}

tasks.register("package") {
group = "Distribution"
description = "Builds all packages"
dependsOn("distTar", "distZip", "buildDeb", "buildRpm")
}
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
group=nrjmx
version=1.6.0-SNAPSHOT
description=The New Relic JMX tool provides a command line tool to connect to a JMX server and retrieve the MBeans it exposes
jmxTermVersion=1.0.2
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit c9b0ae5

Please sign in to comment.