question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jib maven plugin 3.0.0 does not impose mainClass property while 3.1.0 does; but I just want to run a jar file

See original GitHub issue

I have defined this pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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.example</groupId>
        <artifactId>foo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>app</artifactId>
    <properties>
        <docker.registry>some-repo</docker.registry>
        <docker.base.image>${docker.registry}/base/openjdk11:latest</docker.base.image>
        <docker.image>app</docker.image>
        <https.port>8443</https.port>
        <work.dir>/home/wiremock</work.dir>
        <jar>wiremock-jre8-standalone-${wiremock.standalone.version}.jar</jar>
        <full.path>${work.dir}/${jar}</full.path>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock-jre8-standalone</artifactId>
            <version>${wiremock.standalone.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven.dependency.plugin.version}</version>
                <executions>
                    <execution>
                        <!-- make sure the phase here is before the phase to which jib build is attached to -->
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.basedir}/libs</outputDirectory>
                            <includeArtifactIds>wiremock-jre8-standalone</includeArtifactIds>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>${jib.plugin.version}</version> <!-- <---------- here we can define 3.0.0 and 3.1.0, which leads to build success/build error -->
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <from>
                        <image>${docker.base.image}</image>
                    </from>
                    <to>
                        <image>${docker.image}</image>
                        <tags>
                            <tag>latest</tag>
                        </tags>
                    </to>
                    <extraDirectories>
                        <paths>
                            <path>
                                <from>libs/*.jar</from>
                                <into>${work.dir}</into>
                            </path>
                            <path>
                                <from>src/main/resources/stubs/__files</from>
                                <into>${work.dir}/__files</into>
                            </path>
                            <path>
                                <from>src/main/resources/stubs/mappings</from>
                                <into>${work.dir}/mappings</into>
                            </path>
                        </paths>
                        <permissions>
                            <permission>
                                <file>${work.dir}/**/*.*</file>
                                <mode>744</mode>
                            </permission>
                        </permissions>
                    </extraDirectories>
                    <container>
                        <appRoot>${work.dir}</appRoot>
                        <workingDirectory>${work.dir}</workingDirectory>
                        <ports>
                            <port>8080</port>
                            <port>${https.port}</port>
                        </ports>
                        <entrypoint>java,-jar,${full.path},--https-port,${https.port},--verbose</entrypoint>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

What I want to do, is to run java -jar wiremock-xxx.jar in my image.

When the plugin version is 3.0.0, the build and run succeeds. But, when I change to 3.1.0, mvn jib:dockerBuild errors out:

[ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:3.1.0:dockerBuild (default-cli) on project app: Main class was not found, perhaps you should add a `mainClass` configuration to jib-maven-plugin -> [Help 1]

I know running a jar is not the most recommended way of using jib, but I think jib should allows it. And 3.0.0 to 3.1.0 is a minor version upgrade, there should not be any breaking change.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chanseokohcommented, Jun 28, 2021

I looked at your pom.xml again, and here’s what is happening exactly.

The following is not working as you intended and is being ignored. <from> must be a directory and cannot be a glob pattern. You should remove this copy configuration. Believe me, it’s being ignored and thus unnecessary.

                            <path>
                                <from>libs/*.jar</from>
                                <into>${work.dir}</into>
                            </path>

Therefore, it’s also unnecessary to define maven-dependency-plugin like below, whose intention I believe was to prepare <project>/libs for the broken <extraDirectories> copy above.

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven.dependency.plugin.version}</version>
                <executions>
                    <execution>
                        <!-- make sure the phase here is before the phase to which jib build is attached to -->
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.basedir}/libs</outputDirectory>
                            <includeArtifactIds>wiremock-jre8-standalone</includeArtifactIds>
                        </configuration>
                    </execution>
                </executions>

At the same time, Jib is copying all the project dependencies to <appRoot>/libs (including wiremock-jre8-standalone-<version>.jar), which in your case is /home/wiremock/libs. (If you haven’t known, <appRoot>/libs, <appRoot>/classes, and <appRoot>/resoureces are the hard-coded directories where Jib copies dependencies, .class files, and resources, respectively.) That is, the wiremock-jre8-standalone-<version>.jar is at /home/wiremock/libs, not /home/wiremock. I actually confirmed this using your pom.xml. Therefore, java -jar in the entrypoint is pointing to a wrong location:

<full.path>${work.dir}/${jar}</full.path>
...
<entrypoint>java,-jar,${full.path}

The <full.path> should be <full.path>${work.dir}/libs/${jar}</full.path>.

You can use the dive tool to examine the contents of your build image, so check it out. It’s very handy.


What I don’t know is if running with maven, can I just run my dependency jar with packaged mode or not, haven’t tried that.

By now, you should have figured it out, but the mode doesn’t matter. Jib always copies all the dependency JARs (including wiremock-jre8-standalone-<version>.jar) into <appRoot>/libs. The mode is only for your application JAR, not dependencies.

1reaction
chanseokohcommented, Jun 22, 2021

Sorry for the trouble. It’s a regression introduced in 3.1.0. Use 3.1.1, you should be all good.

Closing as a dup of #3295.

Read more comments on GitHub >

github_iconTop Results From Across the Web

After upgrading jib-maven-plugin from 3.0.0 to 3.1.0 entrypoint ...
Execute the following command: mvn package and Docker image is built successfully. Change jib-maven-plugin version to 3.1.0 , execute mvn ...
Read more >
Unable to find a suitable main class, please add a 'mainClass ...
I have a multi module app using Spring Boot and only one Application.java (with main method). I was struggling with it trying to...
Read more >
Management & Monitoring - Micronaut Documentation
Micronaut is a modern, JVM-based, full stack Java framework designed for building ... doesn't run automatically, only when you run one of its...
Read more >
Build Tool Plugins - Spring
The Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an ...
Read more >
Packaging - Micronaut Maven Plugin –
If the <packaging> is set to jar , this plugin will delegate to the maven-shade-plugin to produce a JAR file. Its configuration is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found