当前位置:首页 > 随笔 > 正文内容

Configuring Excludes in scala-maven-plugin

访客 随笔 2026年5月24日 3

When using the scala-maven-plugin, you might need to exclude specific Scala files from compilation. For example, if you have two Scala files—Spark_1.6.1.scala and Spark_2.0.1.scala—in src/main/scala/test and want to exclude the first one, you need to configure the excludes properly.

A common mistake is assuming the exclude pattern supports full path matching. However, scala-maven-plugin only applies patterns to paths inside /test/scala/ or /main/scala/. Therefore, you must use package-style patterns relative to these base directories.

Below is a correct configuration example that excludes all files under the package com.excludes_package and its subpackages:

<excludes>
    <exclude>**/com/excludes_package/**</exclude>
    <exclude>**/com/excludes_package/excludes_package_1/**</exclude>
    <exclude>**/com/excludes_package/excludes_package_2/**</exclude>
</excludes>

Important considerations:

  • Pattern scope: Patterns like /test/ are invalid. Always base patterns on the path starting from src/main/scala/ or src/test/scala/.
  • Incremental mode: Contrary to some earlier reports, incremental compilation (via <recompileMode>incremental</recompileMode>) does support excludes. You can safely enable it.
  • Java compilation: To prevent errors during the Java build phase, you should also add the same excludes to the maven-compiler-plugin configuration. This ensures that excluded Scala files are not inadvertently compiled by the Java compiler.

Here is a complete, working configuration for a mixed Java/Scala project:

<!-- Scala plugin -->
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.2.2</version>
    <executions>
        <execution>
            <id>eclipse-add-source</id>
            <goals>
                <goal>add-source</goal>
            </goals>
        </execution>
        <execution>
            <id>scala-compile-first</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>scala-test-compile-first</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
        </execution>
        <execution>
            <id>attach-scaladocs</id>
            <phase>verify</phase>
            <goals>
                <goal>doc-jar</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <scalaVersion>${scala.version}</scalaVersion>
        <recompileMode>incremental</recompileMode>
        <useZincServer>true</useZincServer>
        <args>
            <arg>-unchecked</arg>
            <arg>-deprecation</arg>
            <arg>-feature</arg>
            <arg>-language:postfixOps</arg>
        </args>
        <jvmArgs>
            <jvmArg>-Xms1024m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
            <jvmArg>-XX:ReservedCodeCacheSize=${CodeCacheSize}</jvmArg>
        </jvmArgs>
        <javacArgs>
            <javacArg>-source</javacArg>
            <javacArg>${java.version}</javacArg>
            <javacArg>-target</javacArg>
            <javacArg>${java.version}</javacArg>
            <javacArg>-Xlint:all,-serial,-path</javacArg>
        </javacArgs>
        <excludes>
            <exclude>**/com/excludes_package/**</exclude>
            <exclude>**/com/excludes_package/excludes_package_1/**</exclude>
            <exclude>**/com/excludes_package/excludes_package_2/**</exclude>
        </excludes>
    </configuration>
</plugin>

<!-- Java compiler plugin with matching excludes -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <encoding>UTF-8</encoding>
        <maxmem>1024m</maxmem>
        <fork>true</fork>
        <compilerArgs>
            <arg>-Xlint:all,-serial,-path</arg>
        </compilerArgs>
        <excludes>
            <exclude>**/com/excludes_package/**</exclude>
            <exclude>**/com/excludes_package/excludes_package_1/**</exclude>
            <exclude>**/com/excludes_package/excludes_package_2/**</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

With this setup, the **/com/excludes_package/** patterns will correctly exclude all matching files from both Scala and Java compilation. Remember that the exclude path is relative to the source root (e.g., src/main/scala), not the full filesystem path.

相关文章

可以按小时收费的VPS

很多 VPS 提供商都支持 按小时计费(hourly billing),想短期试用 / 临时搭建节点、测试网络、短期项目等场景非常合适。下面是当前最主流且靠谱的按小时 VPS 选项,分别按不同需求场景整理: 1. Vultr(全球节点,包括日本) 按小时计费 可选机房:东京 / 大阪 / 洛杉矶 / 法兰克福 / 伦敦 … 支持 PayPal(部分情况),但更常用信用卡/PayPal+卡价格参考$...

在 iPhone 上下载国外App

地区/国家限制App Store 会根据 Apple ID 的国家或地区限制应用下载。如果你的 Apple ID 绑定的是中国大陆,就可能无法下载 OpenAI 官方的 ChatGPT 应用,因为它在大陆 App Store 不上架。解决办法:换成美国、加拿大、香港等地区的 Apple ID。或者在现有 Apple ID 上更改地区。注册一个国外 Apple ID(推荐)比如注册 美国区 Appl...

Node.js 中的异步编程:回调与 Promise

Node.js 是一个基于 JavaScript 构建的单线程、非阻塞运行环境,它通过异步编程机制来高效处理多个操作。在执行如文件读取、API 请求或数据库查询等任务时,Node.js 不会等待这些操作完成,而是使用回调函数和 Promise 来避免阻塞主线程。 回调方式实现异步 那么当异步操作完成后,Node.js 如何知道接下来要做什么呢?这就要用到 回调函数(callback)。 回调本质上...

Selenium自动化测试入门指南

Selenium自动化测试入门指南

什么是自动化测试? 自动化测试是指利用软件工具自动执行测试用例,模拟用户操作,如打开网页、点击链接、输入文本等,并验证结果是否符合预期。 其主要优点包括: 大幅减少人工成本 测试速度快 可以在非工作时间运行 支持持续集成和交付 然而,它也存在一些局限性,例如开发成本较高、不适合快速变化的项目、依赖稳定的UI界面等。 自动化测试的应用条件 适合引入自动化测试的情况包括: 手动测试耗时且需要大量...

MariaDB Galera集群故障快速恢复指南

OpenStack控制节点采用三节点MariaDB Galera集群架构。当数据库集群因故障重启时,有时会出现Galera集群无法正常启动的问题。虽然有多种方法可以恢复数据库服务,但如何实现快速启动同时确保数据完整性呢? 通过分析日志发现,MariaDB Galera集群节点宕机时会在日志中输出以下信息: [Note] WSREP: 新集群视图:全局状态: 874d8e7e-5980-11e8-8...

Android 中 EventBus 的通信机制与实现原理深度解析

EventBus 核心设计思想 EventBus 是一个基于观察者模式的事件总线框架,广泛应用于 Android 平台以实现组件解耦。它通过中心化的消息分发机制,使不同层级、不同线程的对象能够以"发布-订阅"方式通信,避免了传统接口回调或广播带来的强依赖问题。 核心角色说明 事件(Event):任意 Java 对象,作为数据载体,如网络状态变更通知、用户登录信息等。 发布者(Publi...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。