Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"caches": {
"maven": {
"directory": ".m2/repository",
"type": "shared"
}
},
"deploy": {
"base": {
"image": "ghcr.io/railwayapp/railpack-runtime:latest"
},
"inputs": [
{
"include": [
"/mise/shims",
"/mise/installs",
"/usr/local/bin/mise",
"/etc/mise/config.toml",
"/root/.local/state/mise"
],
"step": "packages:mise:runtime"
},
{
"include": [
"."
],
"step": "build"
}
],
"startCommand": "java $JAVA_OPTS -jar $(find */target -name '*-jar-with-dependencies.jar' -o -name '*-spring-boot*.jar' | head -1)"
},
"steps": [
{
"assets": {
"mise.toml": "[mise.toml]"
},
"commands": [
{
"path": "/mise/shims"
},
{
"customName": "create mise config",
"name": "mise.toml",
"path": "/etc/mise/config.toml"
},
{
"cmd": "sh -c 'mise trust -a \u0026\u0026 mise install'",
"customName": "install mise packages: java, maven"
}
],
"inputs": [
{
"image": "ghcr.io/railwayapp/railpack-builder:latest"
}
],
"name": "packages:mise",
"variables": {
"MISE_CACHE_DIR": "/mise/cache",
"MISE_CONFIG_DIR": "/mise",
"MISE_DATA_DIR": "/mise",
"MISE_INSTALLS_DIR": "/mise/installs",
"MISE_NODE_VERIFY": "false",
"MISE_SHIMS_DIR": "/mise/shims"
}
},
{
"caches": [
"maven"
],
"commands": [
{
"cmd": "mvn -DoutputFile=target/mvn-dependency-list.log -B -DskipTests clean dependency:list install -Pproduction"
}
],
"inputs": [
{
"step": "packages:mise"
},
{
"include": [
"."
],
"local": true
}
],
"name": "build",
"secrets": [
"*"
]
},
{
"assets": {
"mise.toml": "[mise.toml]"
},
"commands": [
{
"path": "/mise/shims"
},
{
"customName": "create mise config",
"name": "mise.toml",
"path": "/etc/mise/config.toml"
},
{
"cmd": "sh -c 'mise trust -a \u0026\u0026 mise install'",
"customName": "install mise packages: java"
}
],
"inputs": [
{
"image": "ghcr.io/railwayapp/railpack-builder:latest"
}
],
"name": "packages:mise:runtime",
"variables": {
"MISE_CACHE_DIR": "/mise/cache",
"MISE_CONFIG_DIR": "/mise",
"MISE_DATA_DIR": "/mise",
"MISE_INSTALLS_DIR": "/mise/installs",
"MISE_NODE_VERIFY": "false",
"MISE_SHIMS_DIR": "/mise/shims"
}
}
]
}
12 changes: 10 additions & 2 deletions core/providers/java/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (p *JavaProvider) Plan(ctx *generate.GenerateContext) error {
outPath := "target/."
if ctx.App.HasMatch("**/build/libs/*.jar") || p.usesGradle(ctx) {
outPath = "."
} else if p.isMultimoduleMaven(ctx) {
outPath = "."
ctx.Logger.LogWarn("Multi-module Maven project detected. If the build fails to find the correct JAR, specify a custom start command.")
}

ctx.Deploy.AddInputs([]plan.Layer{
Expand All @@ -81,9 +84,14 @@ func (p *JavaProvider) Plan(ctx *generate.GenerateContext) error {
func (p *JavaProvider) getStartCmd(ctx *generate.GenerateContext) string {
if p.usesGradle(ctx) {
buildGradle := p.readBuildGradle(ctx)
// TODO this seems like a hack? Why are we doing it like this? Let's make sure this is tested and then we can refactor
return fmt.Sprintf("java $JAVA_OPTS -jar %s $(ls -1 */build/libs/*jar | grep -v plain)", getGradlePortConfig(buildGradle))
} else if ctx.App.HasMatch("pom.xml") {
return fmt.Sprintf("java %s $JAVA_OPTS -jar target/*jar", getMavenPortConfig(ctx))
} else if ctx.App.HasFile("pom.xml") {
portConfig := getMavenPortConfig(ctx)
if p.isMultimoduleMaven(ctx) {
return fmt.Sprintf("java %s $JAVA_OPTS -jar $(find */target -name '*-jar-with-dependencies.jar' -o -name '*-spring-boot*.jar' | head -1)", portConfig)
}
return fmt.Sprintf("java %s $JAVA_OPTS -jar target/*jar", portConfig)
} else {
return "java $JAVA_OPTS -jar target/*jar"
}
Expand Down
10 changes: 10 additions & 0 deletions core/providers/java/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (

const MAVEN_CACHE_KEY = "maven"

// isMultimoduleMaven detects if this is a multi-module Maven project where JARs
// are built in subdirectories (e.g., app/target/*.jar) rather than at the root.
func (p *JavaProvider) isMultimoduleMaven(ctx *generate.GenerateContext) bool {
pomFile, err := ctx.App.ReadFile("pom.xml")
if err != nil {
return false
}
return strings.Contains(pomFile, "<modules>")
}

func (p *JavaProvider) getMavenExe(ctx *generate.GenerateContext) string {
if ctx.App.HasMatch("mvnw") && ctx.App.HasMatch(".mvn/wrapper/maven-wrapper.properties") {
return "./mvnw"
Expand Down
34 changes: 34 additions & 0 deletions docs/src/content/docs/languages/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ The Java version is determined in the following order:
| ------------------------- | --------------------------- | ------- |
| `RAILPACK_JDK_VERSION` | Override the JDK version | `17` |
| `RAILPACK_GRADLE_VERSION` | Override the Gradle version | `8.5` |

## Multi-Module Projects
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derkoe I don't have a lot of experience with Java. I'm curious what you think of this approach and if this makes sense for your particular use case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derkoe friendly reminder here! I'm planning on merging this in this week and getting it into next week's railpack release.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iloveitaly I've tried it and I think you will have to redo the whole pack implementation for Java.

I've tried it on https://github.com/porscheinformatik/angular-spring-heroes/ and when you look in the container image I can see the following ending in there:

  • the whole .git folder (aka the whole repository)
  • the whole source code for the application
  • node_modules for the whole app (even though not used during runtime)

Here is a screenshot from dive:
grafik

The built app ends up in /app/heroes-webapp/target/heroes-webapp-1.0.0.BUILD-SNAPSHOT.jar but this is not found by the launcher:

❯ docker run -it angular-spring-heroes
Error: -jar requires jar file specification


Railpack detects multi-module Maven and Gradle projects (identified by
`<modules>` in the root `pom.xml` or multiple subprojects in Gradle).

For multi-module projects, Railpack finds the JAR to run using this
heuristic:

1. First, looks for `*-jar-with-dependencies.jar` (Maven Assembly Plugin)
2. Then, looks for `*-spring-boot*.jar` (Spring Boot Maven Plugin)
3. Finally, picks the first JAR matching `*/target/*.jar` for Maven or
`*/build/libs/*.jar` for Gradle

This heuristic may not work if:

- Your project has multiple executable JARs
- JARs don't follow standard naming conventions
- The build produces only thin JARs (without bundled dependencies)

If JAR selection fails, specify a custom start command.

## Custom Start Command

To override the default start command, create a `railpack.json` file in
your project root:

```json
{
"deploy": {
"startCommand": "java -jar server/target/my-app.jar"
}
}
```
2 changes: 2 additions & 0 deletions examples/java-maven-multimodule/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!test/
target/
47 changes: 47 additions & 0 deletions examples/java-maven-multimodule/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>
<parent>
<groupId>com.railpack</groupId>
<artifactId>java-maven-multimodule</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>java-maven-app</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>java-maven-lib</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.railpack.app.App
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.railpack.app;

import com.railpack.lib.Util;

/**
* Hello world!
*/
public class App {

public static void main(String[] args) {
System.out.println(Util.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.railpack.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

/**
* Unit test for simple App.
*/
public class AppTest {

/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}
11 changes: 11 additions & 0 deletions examples/java-maven-multimodule/lib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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>
<parent>
<groupId>com.railpack</groupId>
<artifactId>java-maven-multimodule</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>java-maven-lib</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.railpack.lib;

public class Util {

public static String getMessage() {
return "Hello Railpack!";
}
}
Loading