How to compile a single file with maven?

14,290

Solution 1

To my knowledge, this is not possible on the the command line. And it is worth noting that the (optional) parameters excludes and includes that you can define to configure the Compiler Plugin in the pom.xml are deprecated as shown below:

$ mvn help:describe -Dcmd=compiler:compile -Ddetail
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building sandbox
[INFO]    task-segment: [help:describe] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:describe {execution: default-cli}]
[INFO] 'compiler:compile' is a plugin goal (aka mojo).
Mojo: 'compiler:compile'
compiler:compile
  Description: Compiles application sources
  Deprecated. No reason given
  Implementation: org.apache.maven.plugin.CompilerMojo
  Language: java
  Bound to phase: compile

  Available parameters:

    ...

    excludes
      A list of exclusion filters for the compiler.
      Deprecated. No reason given

    ...

    includes
      A list of inclusion filters for the compiler.
      Deprecated. No reason given

    ...

So there is actually no real way to do what you want on the long term.

But this leads to one question. Since Maven uses incremental compilation i.e. it compiles only classes that changed on disk since the last build, why would you need to compile only one class? What is your use case?

Solution 2

You will have to configure the compiler plugin to exclude the other files or to include just that one.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>
Share:
14,290
Andrea Francia
Author by

Andrea Francia

Updated on August 11, 2022

Comments

  • Andrea Francia
    Andrea Francia over 1 year

    I set up a working maven project with mvn archetype:create ... I can compile all the sources with mvn compile but I want to be able to compile also a single file at time.

    There is a way to ask maven to compile an arbitrary single .java file?

    Thanks

    Updates 1:

    I would specify the single file on the command line. I would choose the file at the moment of the command launch. I wouldn't modify the pom.xml.

    Update 2:

    Why I asked this? Because I would use autotest-java with the Vim editor.

    autotest-java is an hack/extension of ZenTest (which is a Ruby application that automatically launch unit tests on every save of a Ruby file).

    autotest-java requires a maven project structure and requires that your editor/IDE perform "compile on save". In fact it doesn't recognize when a .java file is modified but instead it monitors for .class files for modifications.

    I succesfully used autotest-java with Netbeans (which support "compile on save") but I would use it with Vim.

  • Andrea Francia
    Andrea Francia over 14 years
    Thanks, but I would specify the single file on the command line. I would choose the file at the moment of the command launch. I wouldn't modify the pom.xml.
  • rodrigoap
    rodrigoap over 14 years
    In the plugin's configuration add a variable instead of a file path.
  • Andrea Francia
    Andrea Francia over 14 years
    Thanks for the reply. My use case is use autotest-java with vim.
  • Andrea Francia
    Andrea Francia over 14 years
    Thanks. Could you explain how?