Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Working with Bundler
      • Rails Support
    • Python
      • Working with Django
      • Background Jobs in Python
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Language Support
  • Java
  • Java Advanced Topics
  • Reducing the Slug Size of Java Applications

Reducing the Slug Size of Java Applications

English — 日本語に切り替える

Last updated December 16, 2019

Table of Contents

  • Using the Maven Clean plugin to remove build artifacts
  • Using doLast in Gradle
  • Excluding test dependencies
  • Adding a .slugignore file
  • Using the Heroku Maven plugin

When deploying with Git, it’s common for very large Java applications to exceed the slug size limits of the platform. If this happens, you’ll see an error like this when you deploy:

remote: -----> Compressing...
remote:  !   Compiled slug size 583.3MB is too large, max is 500 MB.

There are a number of ways to mitigate this problem, including switching to non-Git deployment. In this article, you’ll learn how to do this and how to configure your application to reduce it’s slug size.

Using the Maven Clean plugin to remove build artifacts

The Maven Clean plugin can be configured to remove non-essential artifacts after the build is complete. For example, if your application is packaged into a WAR file, and you would like remove all JAR files (because they are duplicated inside the WAR file), then you could configure the plugin like this in your pom.xml:

<plugin>
  <artifactId>maven-clean-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <id>clean-jar-artifacts</id>
      <phase>install</phase>
      <goals><goal>clean</goal></goals>
      <configuration>
        <excludeDefaultDirectories>true</excludeDefaultDirectories>
        <filesets>
          <fileset>
            <directory>target/dependency/*.jar</directory>
          </fileset>
        </filesets>
      </configuration>
    </execution>
  </executions>
</plugin>

This removes all target/dependency/*.jar files from the slug after the install phase.

Other common patterns for the <configuration> element include removing everything in the target/ directory except the WAR file:

<fileset>
  <directory>target</directory>
  <excludes>
    <exclude>*.war</exclude>
  </excludes>
</fileset>

Or removing Node.js dependencies that were only needed to precompile JavaScript assets:

<fileset>
  <directory>node_modules</directory>
</fileset>
<fileset>
  <directory>.heroku/node</directory>
</fileset>

This configuration is particularly common when the Java buildpack is combined with the Node.js buildpack to perform JavaScript optimization during the build.

Using doLast in Gradle

For Gradle deployments, you can create a stage task with a doLast directive similar to this:

task stage {
    dependsOn build
    doLast {
        delete fileTree(dir: "build", exclude: "libs")
        delete fileTree(dir: "build/libs", exclude: "*.jar")
    }
}

The files you remove will depend on your application.

Excluding test dependencies

If you are using the maven-dependency-plugin to copy JAR files into the slug at build time, you may want to exclude your test dependencies. You can do this by setting the <includeScope> value in the plugin’s configuration:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals><goal>copy-dependencies</goal></goals>
      <configuration>
        <includeScope>compile</includeScope>
      </configuration>
    </execution>
  </executions>
</plugin>

The included scopes defaults to test, which includes your test dependencies, so compile will actually exclude them.

Adding a .slugignore file

If your Git repository includes binaries or other large files that are not needed at runtime, you can exclude them from the slug by creating a .slugignore file in the root of your Git repo.

It is very common for the Git repository to include large files that are only needed when running tests. In this case, the .slugignore file might look like this:

*.psd
*.pdf
/test

You can inspect the extracted contents of your slug by running heroku run bash and using commands such as ls and du. This may help you identify large files that need to be added to the .slugignore file. Exactly what should be excluded depends on the needs of each application.

Using the Heroku Maven plugin

The heroku-maven-plugin avoids Git deployment altogether and builds the slug locally before pushing it to Heroku. This allows it to package only what is absolutely necessary for the application to run.

If you need to include additional directories in the slug, you can do so with the <includes> element. For example:

<includes>
  <include>etc/readme.txt</include>
</includes>

If you implement these suggestions, and still have a slug that is too large, please contact Heroku support.

Keep reading

  • Java Advanced Topics

Feedback

Log in to submit feedback.

Warming Up a Java Process Run Non-web Java Dynos on Heroku

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences