7

Is there way to skip generate-sources in Maven?

Doing it via command line options

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Why would you like to do that? What have bound to the phase? Show your pom file? – khmarbaise Mar 04 '14 at 13:11
  • I dont want regenerate the source as there are no schema changes in files – kuhajeyan Mar 04 '14 at 14:01
  • Its just an example, you can use similar mechanism... – Karthik Prasad Mar 04 '14 at 14:11
  • Ok You can do the generation via a profile put that into a profile called "gen-schema" and afterwards put the generated source into version control. If you need to change the schema just activate the profile. – khmarbaise Mar 04 '14 at 14:59
  • Committing generated code to version control is a bad idea as it might become out of sync with the schema. – marcv81 Nov 02 '15 at 04:58
  • @khmarbaise, in answer to "why would you like to do that": some plugins are broken and take a lot of time to do nothing for incremental builds, which breaks the build-compile-test cycle. – marcv81 Nov 02 '15 at 04:59
  • @marcv81 Can you describe what exactly is broken? Apart from that which version of the plugins do you use? – khmarbaise Nov 02 '15 at 10:15
  • @khmarbaise: In the context of incremental builds I used "broken" to mean that the plugin does more than it should and is hence slow. What is broken is the execution speed, not the build output. Apologies if there was any confusion. On one of my CXF projects Maven would take 2+ minutes for incremental builds with no code change. In the context of the build-compile-test cycle I used "broken" to mean that it becomes extremely difficult for developers to reach the zone as they are constantly waiting for compilation results. Again, what is broken is productivity, but not the build output. – marcv81 Nov 02 '15 at 11:09
  • The first question is if it really related maven-resources-plugin if you are talking about generation (via CXF) what does cxf-maven-plugin does? Do you have an example project which shows the problem? This would be very helpful. Apart from that you should file in an appropriate feature [request/improvement](https://issues.apache.org/jira/browse/MRESOURCES/) or you can take a look if [this](http://takari.io/book/60-incremental.html#incremental-build-support-library) is something which you can use. – khmarbaise Nov 02 '15 at 17:23
  • @khmarbaise: It's not related to maven-resources-plugin! I don't know how you understood it was, as this plugin does not seem to be mentioned anywhere here. In any case several Maven plugins (including cxf-codegen-plugin) are way too slow when doing incremental rebuilds, which is why it is useful to sometimes skip certain phases. I hope this answers your initial question :) I agree that it should be fixed directly in the impacted plugins, but unfortunately we can't always wait or do it ourselves. – marcv81 Nov 03 '15 at 03:42
  • @marcv81 To get it fixed sometimes you need to do a request there otherwise the plugin developers don't know that there a problem exist or at least a wish for improvement...I understand of course that you can't wait until it's fixed... – khmarbaise Nov 03 '15 at 09:43
  • @khmarbaise: You are correct. In my particular case I found an existing task marked as "won't fix", so I did not raise a duplicate. I indeed encourage everybody to raise the bugs they find. This said I have the feeling incremental builds are a second class citizen in the Maven world. Nevertheless Maven was a significant improvement over Ant, and it set the stage for the modern and performance-focused build systems we use now. – marcv81 Nov 03 '15 at 15:27
  • Can you mention the issue number? – khmarbaise Nov 03 '15 at 16:10

3 Answers3

4

I've scenario where I generate CXF classes when ever I there is change in WSDL or WADL. Hence I generate it explicitly whenever I need. Hence I created a separate profile a new profile cxf-gen along with my usual dev, uat, syst. which has plugins to generate the classes. In short whenever I need to regenerate the classes I switch to the profile and run generate-sources. Here is sample profile I use.

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <envName>dev</envName>
        </properties>
    </profile>
    <profile>
        <id>uat</id>
        <properties>
            <envName>uat</envName>
        </properties>
    </profile>

    <profile>
        <id>jaxB-gen</id>
        <properties>
            <envName>dev</envName>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>jaxb2-maven-plugin</artifactId>
                    <version>1.5</version>
                    <configuration>
                        <!-- CONFIGS ->
                    </configuration>
                    <executions>
                        <execution>
                            <id>xjc</id>
                            <goals>
                                <goal>xjc</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>code-gen</id>
        <properties>
            <envName>dev</envName>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-codegen-plugin</artifactId>
                    <version>${cxf.version}</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <!-- CONFIGS ->
                            </configuration>
                            <goals>
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- wadl2java Required only when JAXRS classes are to be generated -->
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-wadl2java-plugin</artifactId>
                    <version>2.7.6</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <!-- CONFIGS ->
                            </configuration>
                            <goals>
                                <goal>wadl2java</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>com.googlecode.jsonschema2pojo</groupId>
                    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                    <version>0.3.7</version>
                    <configuration>
                        <!-- CONFIGS ->
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>
        </build>
    </profile>
</profiles>
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112
  • This almost a good answer. The problem is that it should generate the source by default and not on demand. Otherwise a naive clean build (mvn compile) will be broken by default. Good docs could fix this, but better fix it in the POM. – marcv81 Nov 02 '15 at 05:02
  • The answer matches the question though : "Is there way to skip generate-sources in Maven? Doing it via command line options". By default it's skipped, and when u want to do it via command line you do something like "mvn jaxb2:xjc -P jaxB-gen" – Tristan Mar 05 '18 at 20:53
  • 1
    @Tristan There is a field in plugin, you can set it to ${cxf.skip} and then from command line you can pass -Dcxf.skip=true – Karthik Prasad Mar 06 '18 at 06:21
4

This command line option should work if you are using maven-source-plugin (works with Maven 3.6.0):
-Dmaven.source.skip=true

boot-and-bonnet
  • 731
  • 2
  • 5
  • 16
2

This is an old question and although some answers would somehow work none of them are ideal.

This answer does not break clean builds: calling "mvn <goal>" still produces the expected and backward-compatible result, which is good for continuous integration. Also this answer does not rely on committing generated code to version control, which is a bad idea as it might become out of sync with the source.

I am assuming the generate-sources phase is bound to a plugin goal.

The answer is to create a profile called "clean-build" which is active by default and contains your plugin binding. When a developer trusts they can safely skip generate-sources they may run the following.

mvn -P !clean-build <goal>

Or if the exclamation mark needs to be escaped.

mvn -P \!clean-build <goal>

Here is what the pom.xml might look like.

<profiles>
  <profile>
    <id>clean-build</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
      <plugins>
        <plugin>
          ...
          <executions>
            <execution>
              ...
              <phase>generate-sources</phase>
              ...
            </execution>
          </executions>
          ...
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

This answer requires Maven 2.0.10+.

marcv81
  • 850
  • 8
  • 22