Add Maven generated site to generated package

左心房为你撑大大i 提交于 2019-12-03 21:13:19

Using site:jar and then the assembly plugin to create a bundle of everthing should be possible.

I'm posting here to document a solution for building a war file with the maven generated site included with the recent Maven 3. I have wrapped the required build plugins into a profile which is activated by Hudson setting the BUILD_NUMBER system property. In addition to adding the site directory to the war, this configuration generates a -javadoc.jar artifact (the separate maven-javadoc-plugin with the attach-javadocs execution is responsible for that, and you can remove it if not required).

<profiles>
    <!-- We want the javadoc jar and the site within the war only when building on Hudson -->
    <profile>
        <activation>
            <property>
                <name>BUILD_NUMBER</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.0-beta-3</version>
                    <configuration>
                        <reportPlugins>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </plugin>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-javadoc-plugin</artifactId>
                            </plugin>
                        </reportPlugins>
                    </configuration>
                    <executions> 
                        <execution> 
                            <id>attach-site</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals> 
                        </execution> 
                    </executions> 
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webResources>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                <directory>${project.build.directory}/site</directory>
                                <targetPath>doc</targetPath>
                            </resource>
                        </webResources>
                    </configuration>
                </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>
            </plugins>
        </build>
    </profile>
</profiles>

Here is the same profile, but for Maven2. This is useful if you have a hudson project configuration relying on the maven2 hudson plugin.

<profiles>
    <!-- We want the javadoc jar and the site within the war only when building on Hudson -->
    <profile>
        <activation>
            <property>
                <name>BUILD_NUMBER</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webResources>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                <directory>${project.build.directory}/site</directory>
                                <targetPath>doc</targetPath>
                            </resource>
                        </webResources>
                    </configuration>
                </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>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>2.2</version>
                    <executions> 
                        <execution> 
                            <id>attach-site</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals> 
                        </execution> 
                    </executions> 
                </plugin>
            </plugins>
        </build>
        <reporting>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                </plugin>
            </plugins>
        </reporting>
    </profile>
</profiles>

The trick is to configure maven-site-plugin to generate site in a phase happening before the package (e.g. prepare-package) and then include the generated site into the webapp configuring maven-war-plugin properly.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.5</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>site</goal>
                <goal>stage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <webResources>
            <resource>
                <directory>${basedir}/target/staging</directory>
                <includes>
                    <include>**</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

Here you can find a sample project using this configuration. Here a live demo.

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