42

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
salbeira
  • 2,375
  • 5
  • 26
  • 40

5 Answers5

46
<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

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • 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. – Justin Phillips Nov 21 '19 at 14:37
  • Used this on a jenkins pipeline where i compiled once and tested on parallel. – Alfabravo Apr 07 '22 at 15:54
  • Note: that details on this can be found here (this is NOT the same as skipping unit tests) https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html – atom88 May 19 '22 at 22:22
11

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.

rjmunro
  • 27,203
  • 20
  • 110
  • 132
Matthias
  • 3,458
  • 4
  • 27
  • 46
4

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>
Noam Manos
  • 15,216
  • 3
  • 86
  • 85
  • This worked. Anyway, what is the definition of "incremental compilation"? – panoet May 27 '22 at 01:10
  • 1
    [useIncrementalCompilation](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#useIncrementalCompilation) = false: Only compile source files which have changed since the last compilation. – Noam Manos May 29 '22 at 07:08
0

so I used it like this and it worked. ( false)

<executions>
        <execution>
                    <id>default-compile</id>
                    <configuration>
                        <showDeprecation>true</showDeprecation>
                        <showWarnings>true</showWarnings>
                        <failOnError>false</failOnError>
                        <compilerArgument>-Xlint:unchecked</compilerArgument>
                        <compilerArguments>
                            <source>${maven.compiler.target}</source>
                            <target>${maven.compiler.source}</target>
                        </compilerArguments>
                    </configuration>
        </execution>
Yogi
  • 1
  • 1
-3

I would define a separate pom just for building that separate artifact.

In that pom, you only use your resources generation plugin and the packaging ones.

Then you don't have to worry about it doing too much.

David Pierre
  • 9,459
  • 4
  • 40
  • 32