Skip to content

Commit

Permalink
feat: add demo agent implementations and demo main 🤖
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jul 10, 2023
1 parent 577976e commit ab7fcef
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
java-version: 8
distribution: zulu
- name: test with Java 8
run: ./mvnw -V --no-transfer-progress surefire:test
run: ./mvnw -V --no-transfer-progress dependency:properties surefire:test

- name: 'remove self maven install files(OS: *nix)'
run: rm -rf $HOME/.m2/repository/io/foldright/study*
Expand Down
61 changes: 61 additions & 0 deletions hello-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,65 @@
</parent>

<artifactId>hello-agent</artifactId>

<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>io.foldright.study.hello.agent.HelloAgent</Premain-Class>
<Boot-Class-Path>${project.build.finalName}.jar</Boot-Class-Path>
<Can-Redefine-Classes>false</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Set-Native-Method-Prefix>false</Can-Set-Native-Method-Prefix>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade-when-package</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>io.foldright.study.hello.agent.internal.javassist</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>org.javassist:javassist</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>org.javassist:javassist</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.foldright.study.hello.agent;

import edu.umd.cs.findbugs.annotations.NonNull;

import java.lang.instrument.Instrumentation;


public class HelloAgent {
public static void premain(final String agentArgs, @NonNull final Instrumentation inst) {
System.out.println("Hello Agent!");
}
}
31 changes: 31 additions & 0 deletions main-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,35 @@
</parent>

<artifactId>main-runner</artifactId>

<dependencies>
<dependency>
<groupId>io.foldright.study</groupId>
<artifactId>hello-agent</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.foldright.study</groupId>
<artifactId>world-agent</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:${io.foldright.study:hello-agent:jar}
-javaagent:${io.foldright.study:world-agent:jar}
@{argLine}
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
7 changes: 7 additions & 0 deletions main-runner/src/main/java/io/foldright/study/main/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.foldright.study.main;

public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
11 changes: 11 additions & 0 deletions main-runner/src/test/java/io/foldright/study/main/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.foldright.study.main;

import org.junit.jupiter.api.Test;


class MainTest {
@Test
void hello() {
System.out.println("Hello world!");
}
}
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<!-- A Guide to Maven Encoding https://www.baeldung.com/maven-encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding>
<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.deploy.skip>true</maven.deploy.skip>

<spotbugs.annotations.version>4.7.3</spotbugs.annotations.version>
Expand All @@ -75,6 +76,7 @@
<!-- testing dependencies versions -->
<junit5.version>5.9.3</junit5.version>
<kotest.version>5.6.2</kotest.version>
<argLine/>
</properties>

<dependencies>
Expand Down Expand Up @@ -313,6 +315,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -422,6 +436,11 @@
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
4 changes: 2 additions & 2 deletions scripts/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ for jdk_version in "${JDK_VERSIONS[@]}"; do
# about CI env var
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if [ "${CI:-}" = true ]; then
jvb::mvn_cmd jacoco:prepare-agent surefire:test jacoco:report
jvb::mvn_cmd jacoco:prepare-agent dependency:properties surefire:test jacoco:report
else
jvb::mvn_cmd surefire:test
jvb::mvn_cmd dependency:properties surefire:test
fi
done
61 changes: 61 additions & 0 deletions world-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,65 @@
</parent>

<artifactId>world-agent</artifactId>

<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>io.foldright.study.world.agent.WorldAgent</Premain-Class>
<Boot-Class-Path>${project.build.finalName}.jar</Boot-Class-Path>
<Can-Redefine-Classes>false</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Set-Native-Method-Prefix>false</Can-Set-Native-Method-Prefix>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade-when-package</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>io.foldright.study.world.agent.internal.javassist</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>org.javassist:javassist</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>org.javassist:javassist</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.foldright.study.world.agent;

import edu.umd.cs.findbugs.annotations.NonNull;

import java.lang.instrument.Instrumentation;


public class WorldAgent {
public static void premain(final String agentArgs, @NonNull final Instrumentation inst) {
System.out.println("World Agent!");
}
}

0 comments on commit ab7fcef

Please sign in to comment.