All Collections
Tips and Tricks
Language-specific issues
Publishing Maven projects on Code Ocean
Publishing Maven projects on Code Ocean

How to avoid a common pitfall by baking Java dependencies into the environment

Updated over a week ago

To get a Maven project set up on Code Ocean, the Dockerfile must be edited so that dependencies get installed during build time rather than run time. If you're unfamiliar with Docker, follow the steps outlined below:

  1. Prerequisite: the project must be uploaded to the code folder ensure that any data is separate and placed in the data folder.

  2. In the environment editor, add maven as an apt-get dependency.

  3. Move pom.xml to the environment folder by dragging and dropping it

  4. Click Dockerfile, unlock it for editing, and add the following lines at the end:
    โ€‹COPY pom.xml /tmp
    RUN cd /tmp && mvn package && rm -rf /tmp/target

  5. Make sure code/run, or your master script, has the following lines before the actual commands to run your code:
    โ€‹mv /tmp/pom.xml .
    mvn package --offline

What this will do is instruct Maven to download all dependencies when the image is being built in step 4, and then instruct it to build the project in offline mode, step 5.

Note: Maven doesn't fetch plugin dependencies (see this long-standing Maven bug). Even though the project dependencies are preserved in the image, there's a chance that eventually one of the plugin dependencies will disappear from Maven Central, breaking the build and rendering the results non-reproducible.


Did this answer your question?