简体   繁体   中英

Maven: Exclude generated-sources from jar artifact

Duplicate but no answers and cannot comment on

Background info: I have a module that uses the xmlbeans-maven-plugin to generate java sources files from xsd and compile into its own jar. This works and also creates a module1/target/generated-sources folder with the java sources. the module is built but with the generated sources inside the jar which is unnecessary since the xmlbeans plugin creates a separate jar holding the compile generated-sources

I am trying to exclude the target/generated-sources directory from being packaged into the module's jar artifact. I tried using target/generated-sources/xmlbeans/noNamespace/**/*.java in maven-compiler-plugin and maven-jar-plugin with no success

What am I missing here?

Here is the module's pom.xml, if the parent pom is needed I will post that too:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>parent</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>workflow</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>WorkFlow processing App</name>

<build>
    <finalName>workflow</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>xmlbeans-maven-plugin</artifactId>
            <version>2.3.3</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xmlbeans</goal>
                    </goals>
                </execution>
            </executions>
            <inherited>true</inherited>
            <configuration>
                <schemaDirectory>src/main/xsd</schemaDirectory>
                <outputJar>${build.dir}/lib/WorkflowResponse.jar</outputJar>
            </configuration>

        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${compiler.plugin.version}</version>
            <configuration>
                <source>${compiler.source.version}</source>
                <target>${compiler.target.version}</target>
                <generatedSourcesDirectory>target</generatedSourcesDirectory>
                <!--<includes>-->
                    <!--<include>src/main/java/**/*.java</include>-->
                <!--</includes>-->
                <excludes>
                    <exclude>target/generated-sources/xmlbeans/noNamespace/**/*.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-jar-plugin</artifactId>-->
            <!--<version>2.4</version>-->
            <!--<configuration>-->
                <!--<includes>-->
                    <!--<include>src/main/java/com/company/appname/*.java</include>-->
                <!--</includes>-->
            <!--</configuration>-->
        <!--</plugin>-->
    </plugins>
</build>


****UPDATE******

I have managed to exclude the generated sources but it is ugly and not portable:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <includes>
            <include>com/**</include>
        </includes>

    </configuration>
 </plugin>

This will include only the contents under target/classes/com after compilation has taken place and when maven is packaging the module into a jar which is not clean. Ideally, the exclusion should happen in the compiler so this way the contents of target/generated-sources are not compiled into target/classes/

Similar to how you used includes in your update, there is an excludes element that does what you want. You would just indicate what package you want to exclude instead of include.

http://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM