开发者

Add Maven generated site to generated package

In order to deliver an all-in-one Jar for our project, we are wondering if it is possible to include the project's generated site (generated via the site plugin) into the generated project's package? The idea behind this is very simple, we have a project named "sample" that demonstrates the usage of our API and we want to give the user documentation on the AP开发者_高级运维I and its usage in a single bundle. Is it possible using Maven to create such a Jar?

Thanks in advance!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜