Skip to content

Commit

Permalink
use shadowjar to produce a seft contained jar file for some application
Browse files Browse the repository at this point in the history
  • Loading branch information
jomifred committed Oct 14, 2024
1 parent eb9e6bc commit 2887b9c
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions doc/jason-cli/commands.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ commands to handle applications
Commands:
create creates the files of a new application
compile compiles the java classes of an application (using Gradle)
and produces a jar file with all necessary to run the application
add-agent adds the code of a new agent into the application
add-gradle adds a Gradle script for the application
add-ia adds the source code a new internal action into the application
Expand Down
4 changes: 2 additions & 2 deletions doc/jason-cli/readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Jason CLI is a command-line interface tool that you use to initialize, devel
include::../install.adoc[]


== Create Applications
== Create and Execute Applications

New Jason applications can be created and executed with:

Expand All @@ -23,7 +23,7 @@ The first command creates a Jason application identified by `app1` with two agen
[alice] hello world.
```

This approach uses Gradle to execute the application. Gradle is useful if you are using other packages. However, it you do not have dependencies, the application can be started faster without Gradle:
This approach uses Gradle to execute the application. Gradle is useful if you are using other packages. However, if you do not have dependencies, the application can be started faster without Gradle:

```
jason mas start --mas2j=app1.mas2j
Expand Down
2 changes: 2 additions & 0 deletions doc/release-notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* `jason x.mas2h` uses gradle to run the application
* `jason mas start --mas2j=x.mas2h` runs without gradle. The classpath might be informed using `--cp` if necessary.

* `jason app compile` uses shadowJar to create a jar file with all files and dependencies to run the application.

- update doc for new Jason web site: https://jason-lang.github.io


Expand Down
5 changes: 4 additions & 1 deletion jason-cli/src/main/java/jason/cli/app/Compile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@Command(
name = "compile",
description = "compiles the java classes of the application in the current directory (using Gradle)"
description = "compiles the java classes of the application in the current directory (using Gradle) and produces a jar file with all necessary to run the application"
)
public class Compile extends Common implements Runnable {

Expand All @@ -26,6 +26,9 @@ public void run() {
getGradleBuild(connection)
.forTasks("compileJava")
.run();
getGradleBuild(connection)
.forTasks("shadowJar")
.run();

if (created) {
// delete created files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public int init(String[] args) {
if (RunLocalMAS.class.getResource("/"+defaultProjectFileName) != null) {
projectFileName = defaultProjectFileName;
appFromClassPath = true;
Config.get(false); // to void to call fix/store the configuration in this case everything is read from a jar file
//Config.get(false); // to void to call fix/store the configuration in this case everything is read from a jar file
} else {
if (!(boolean)(initArgs.getOrDefault("empty-mas", false))) {
System.out.println("Jason " + Config.get().getJasonVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public String fixPath(String f) {
if (f.startsWith("$")) { // the case of "$jason/src/a.asl"
String jar = f.substring(1,f.indexOf("/"));
if (Config.get().getPackage(jar) == null) {
System.err.println("The included file '"+jar+"' is not configured");
System.err.println("The included '$"+jar+"' is not configured");
} else {
var nf = "jar:file:" + Config.get().getPackage(jar).getAbsolutePath() + "!" + f.substring(f.indexOf("/"));
if (testURLSrc(nf))
Expand Down
20 changes: 0 additions & 20 deletions jason-interpreter/src/main/java/jason/stdlib/include.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,6 @@
</ul>
*/
@Manual(
literal=".include(file[,namespace])",
hint="loads an .asl file, i.e., includes beliefs, goals, and plans from a file optionally in a namespace",
argsHint= {
"the file name or any valid URL",
"sets the name space where the included components (bels, plans, ...) will be placed [optional]"
},
argsType= {
"string",
"atom or var"
},
examples= {
".include(\"x.asl\")",
".include(\"https://raw.githubusercontent.com/jason-lang/jason/master/examples/auction/ag3.asl\")",
".include(\"jar:file:/Users/jomi/lib/test.jar!/l.asl\") includes from a local jar file."
},
seeAlso= {
""
}
)
@SuppressWarnings("serial")
public class include extends DefaultInternalAction {

Expand Down
4 changes: 3 additions & 1 deletion jason-interpreter/src/main/java/jason/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public boolean tryToFixJarFileConf(String jarEntry, String jarFileNamePrefix) {
return true;
}
} catch (Exception e) {}
if (showFixMsgs)
System.out.println("Configuration of '"+jarEntry+"' NOT found, based on class loader");

// try to get from classpath (the most common case)
jarFile = getJarFromClassPath(jarFileNamePrefix, fileInJar);
Expand All @@ -331,7 +333,7 @@ public boolean tryToFixJarFileConf(String jarEntry, String jarFileNamePrefix) {
return true;
}
if (showFixMsgs)
System.out.println("Configuration of '"+jarEntry+"' NOT found, based on class loader");
System.out.println("Configuration of '"+jarEntry+"' NOT found, based on class path: "+System.getProperty("java.class.path"));

// try with $JASON_HOME
String jh = System.getenv().get("JASON_HOME");
Expand Down
46 changes: 44 additions & 2 deletions jason-interpreter/src/main/resources/templates/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
<DATE>
*/

defaultTasks 'run'
plugins {
id 'com.gradleup.shadow' version '8.3.3'
id 'java'
}

apply plugin: 'java'
defaultTasks 'run'

// set version of group for your project
//version '1.0'
Expand Down Expand Up @@ -60,3 +63,42 @@ task runJade (type: JavaExec, dependsOn: 'classes') {
// and all usual JADE options for jade.Boot
classpath sourceSets.main.runtimeClasspath
}

shadowJar {
doFirst {
copy {
from '<PROJECT-FILE>'
rename '<PROJECT-FILE>','default.mas2j'
into project.projectDir.absolutePath + '/'
}
}
archiveFileName = "jason-${project.name}-all.jar" // the name must start with jacamo so that jacamo...jar is found in the classpath
from (project.projectDir.absolutePath + '/src') {
include '**/*.asl'
include '**/*.xml'
include '**/*.sai'
include '**/*.ptl'
include '**/*.jcm'
include '*.properties'
exclude 'test'
}
from (project.buildDir.absolutePath + '/classes') {
include '**/*'
}
from (project.buildDir.absolutePath + '/jcm') {
include '**/*'
}
from (project.projectDir.absolutePath + '/') {
include '*.properties'
include 'default.mas2j'
}

manifest {
attributes 'Main-Class': 'jason.infra.local.RunLocalMAS'
}

doLast {
println("You can run your application with: java -jar build/libs/jason-${project.name}-all.jar")
}
}

0 comments on commit 2887b9c

Please sign in to comment.