Maven skip compile

53,681

Solution 1

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <executions>
     <execution>
       <id>default-compile</id>
       <phase>compile</phase>
       <goals>
          <goal>compile</goal>
       </goals>
       <configuration>
         <skipMain>true</skipMain> <--Skip
       </configuration>
     </execution>
   </executions>
</plugin>

Set this to 'true' to bypass compilation of main sources. Its use is NOT RECOMMENDED, but quite convenient on occasion. User property is: maven.main.skip.

mvn package -Dmaven.main.skip

Solution 2

Maven functionality is partly organized in plugins, that contains goals. These goals can be executed without being part of a lifecycle. Eg for for the jar-plugin's jar-goal you would invoke:

mvn jar:jar

If you browse through the list of available plugins you will probably find the functionality you are looking for. If it is necessary you could even define an "assembly" to select the files you want to bundle into an archive.

Solution 3

To prevent Maven compilation by default, first make sure that the configuration of maven-compiler-plugin has useIncrementalCompilation = false:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>

Also, in case your Maven Profile uses maven-clean-plugin, then by default it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory.

To disable these default cleanups, add to maven-clean-plugin configuration: excludeDefaultDirectories = true:

<excludeDefaultDirectories>true</excludeDefaultDirectories>
Share:
53,681
salbeira
Author by

salbeira

Updated on June 14, 2021

Comments

  • salbeira
    salbeira almost 3 years

    I want to use Maven to execute a certain plug-in that only needs the source code but I do not want Maven to compile anything (mostly because the project just doesn't compile).

    How do I tell Maven to skip the compile step and just launch its plug-in and then package the generated resources together in a nice JAR? (The procedure of the last step is already known to me.)

    Additional Info:

    So we tried a lot of things right now, e.g.:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
            <source>1.7</source>
                <target>1.7</target>
        </configuration>
        <executions>
            <execution>
                <phase>deploy</phase>
            </execution>
        </executions>
    </plugin>
    

    Though when we do a mvn package we get this:

    [INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ project ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling ALOTOF source files to /home/myname/dir/dir/project/target/classes
    

    message edited ofc.

  • Justin Phillips
    Justin Phillips over 4 years
    This is useful for a situation where I have a POM file that is installing JAR files for a project, but does not need to compile the source, because this is being compiled and executed via different tooling. In this case it is a Gauge Java test.
  • Pierre-Luc Bertrand
    Pierre-Luc Bertrand almost 3 years
    You should add to your answer that it actually compiles but doesn't fail the process.
  • Antonio Petricca
    Antonio Petricca almost 3 years
    You have to better describe your issue.
  • Alfabravo
    Alfabravo about 2 years
    Used this on a jenkins pipeline where i compiled once and tested on parallel.
  • atom88
    atom88 almost 2 years
    Note: that details on this can be found here (this is NOT the same as skipping unit tests) maven.apache.org/plugins/maven-compiler-plugin/…
  • panoet
    panoet almost 2 years
    This worked. Anyway, what is the definition of "incremental compilation"?
  • Noam Manos
    Noam Manos almost 2 years
    useIncrementalCompilation = false: Only compile source files which have changed since the last compilation.