Skip to content

Commit

Permalink
Merge pull request #2 from breadrock1/improve/add-example
Browse files Browse the repository at this point in the history
Improve: Added example
  • Loading branch information
breadrock1 authored Jul 6, 2024
2 parents a01f74a + 1342628 commit 435927f
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 10 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ jobs:
- name: Build rust library
run: |
cargo build --manifest-path adblock-rs/Cargo.toml
mkdir -p ./target/lib
cp -r ./adblock-rs/target/debug/libadblock_coffee.* ./target/lib/
- uses: actions/setup-java@v4
with:
java-version: '11'
Expand Down Expand Up @@ -52,8 +50,6 @@ jobs:
- name: Build rust library
run: |
cargo build --manifest-path adblock-rs/Cargo.toml
mkdir -p ./target/lib
cp -r ./adblock-rs/target/debug/libadblock_coffee.* ./target/lib/
- uses: actions/setup-java@v4
with:
java-version: '11'
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ jobs:
run: |
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android
cargo build --release --manifest-path adblock-rs/Cargo.toml
mkdir -p ./target/lib
cp -r ./adblock-rs/target/release/libadblock_coffee.* ./target/lib/
- uses: actions/setup-java@v4
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**Full Changelog**: https://github.com/breadrock1/Adblock-coffee/compare/adblock-coffee-1.0.3...adblock-coffee-1.0.4
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,62 @@ First, you need to build the `adblock-rust` library and generate the shared libr

1. Clone the `adblock-rust` repository:
2. Build the library:
```sh
```shell
cargo build --release --manifest-path adblock-rs/Cargo.toml
mvn package
mvn install
```

If you want to build library to another platform add option `--target` with needed platform like `aarch64-linux-android`, `armv7-linux-androideabi`, `i686-linux-android`.
After that check that path of built library exists into `pom.xml` `maven-resources-plugin` section. As default `rustup` using current platform and creates `target/{debug,release}` directories.
After switching target platform by `rustup` these directories created as `targer/{target-platform}/{debug,release}` directories.

Example building library for `aarch64-linux-android` target:
```shell
cargo build --release --target aarch64-linux-android --manifest-path adblock-rs/Cargo.toml
```

And you have to update `pom.xml` file:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-native-libs</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/target/lib</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/adblock-rs/target/aarch64-linux-android/debug/</directory>
<includes>
<include>**/*.dylib</include>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/adblock-rs/target/aarch64-linux-android/release/</directory>
<includes>
<include>**/*.dylib</include>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
```

And build jar:
```shell
mvn install
```

3. Locate the generated shared library file in the `target/release` directory.
Expand Down
67 changes: 67 additions & 0 deletions examples/simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.simple</groupId>
<artifactId>simple</artifactId>
<version>1.0.1</version>
<name>simple</name>

<properties>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.example.simple.App</start-class>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example.adblock</groupId>
<artifactId>adblock-coffee</artifactId>
<version>1.0.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/adblock-coffee-1.0.3.jar</systemPath>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
com.example.simple.App
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>-Djava.library.path=${project.basedir}/target/lib/</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
29 changes: 29 additions & 0 deletions examples/simple/src/main/java/com/example/simple/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.simple;

import java.util.ArrayList;
import java.util.List;

import com.example.adblock.AdvtBlocker;

/**
* Simple example!
*/
public class App {
public static void main(String[] args) {
List<String> rules = new ArrayList<>(List.of(
"-advertisement-icon.",
"-advertisement-management/",
"-advertisement.",
"-advertisement/script."
));

AdvtBlocker blocker = AdvtBlocker.createInstance(rules);
boolean result = blocker.checkUrls(
"http://example.com/-advertisement-icon.",
"http://example.com/helloworld",
"image"
);

System.out.println(result);
}
}
37 changes: 37 additions & 0 deletions examples/simple/src/test/java/com/example/simple/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.simple;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.example.adblock.AdvtBlocker;

/**
* Simple test of using library
*/
public class AppTest {
/**
* Rigorous Test :-)
*/
@Test
public void mainTest() {
List<String> rules = new ArrayList<>(List.of(
"-advertisement-icon.",
"-advertisement-management/",
"-advertisement.",
"-advertisement/script."
));

AdvtBlocker blocker = AdvtBlocker.createInstance(rules);
boolean result = blocker.checkUrls(
"http://example.com/-advertisement-icon.",
"http://example.com/helloworld",
"image"
);

assertTrue(result);
}
}
46 changes: 44 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.example.adblock</groupId>
<artifactId>adblock-coffee</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
<name>adblock-coffee</name>

<properties>
Expand Down Expand Up @@ -59,7 +59,15 @@
<outputDirectory>${project.basedir}/target/lib</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/lib</directory>
<directory>${project.basedir}/adblock-rs/target/debug/</directory>
<includes>
<include>**/*.dylib</include>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/adblock-rs/target/release/</directory>
<includes>
<include>**/*.dylib</include>
<include>**/*.so</include>
Expand All @@ -71,6 +79,40 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.adblock.AdvtBlocker</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.basedir}/target/lib/libadblock_coffee.dylib</source>
<source>${project.basedir}/target/lib/libadblock_coffee.so</source>
<source>${project.basedir}/target/lib/libadblock_coffee.dll</source>
<outputDirectory>${project.basedir}/target/</outputDirectory>
</file>
</files>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 435927f

Please sign in to comment.