Deep-dive on the Next Gen Platform. Join the Webinar!

Skip Navigation
Show nav
Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • 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
Hide categories

Categories

  • Heroku Architecture
    • Compute (Dynos)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Developer Tools
    • Command Line
    • Heroku VS Code Extension
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery & Integration (Heroku Flow)
    • Continuous Integration
  • Language Support
    • Node.js
      • Working with Node.js
      • Troubleshooting Node.js Apps
      • Node.js Behavior in Heroku
    • Ruby
      • Rails Support
      • Working with Bundler
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Background Jobs in Python
      • Python Behavior in Heroku
      • Working with Django
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Working with Maven
      • Working with Spring Boot
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
      • Migrating to Heroku Postgres
    • Heroku Key-Value Store
    • Apache Kafka on Heroku
    • Other Data Stores
  • AI
    • Working with AI
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
      • Single Sign-on (SSO)
    • Private Spaces
      • Infrastructure Networking
    • Compliance
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
  • 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
  • Working with Java
  • Reducing the Slug Size of Java Applications

Reducing the Slug Size of Java Applications

English — 日本語に切り替える

Last updated December 09, 2024

Table of Contents

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

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. In this article, you’ll learn how to configure your application to reduce it’s slug size.

This article is only applicable to apps that use classic buildpacks.

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.

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

Keep reading

  • Working with Java

Feedback

Log in to submit feedback.

Warming Up a Java Process Running Database Migrations for Java Apps

Information & Support

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

Language Reference

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

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices