How to generate a WAR with the source code in Maven?

浪子不回头ぞ 提交于 2019-11-30 08:54:23

It is possible configure the maven-war-plugin to include the source directory as it was a web resource:

 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webResources>
            <resource>
              <directory>${build.sourceDirectory}</directory>
              <targetPath>sources</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

The java sources will be included in a sources directory in the war. Of course you should adapt the resource directory to your own maven layout.

If you want the source files in the same directory as the class files you would use:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${build.sourceDirectory}</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

Usually I think you would go this way: (this won't include the source files, but provides them as separate files)

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

At your war project's pom.xml:

<build>
    ...
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <attachClasses>true</attachClasses>
                    <classesClassifier>classes</classesClassifier>
                </configuration>
            </plugin>
            ...
        </plugins>
    </pluginManagement>
</build>

In the projects you want do use it:

<dependency>
    <groupId>my-war-group</groupId>
    <artifactId>my-war-artifact-id</artifactId>
    <version>my-war-version</version>

    <classifier>classes</classifier> <!-- THIS IS THE IMPORTANT LINE! -->
</dependency>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!