多模块POM - 创建有效的网站

时间:2012-06-01 10:34:17

标签: maven maven-site-plugin

我有一个多模块应用程序,我正在尝试为此应用程序生成一个maven站点。

我有一个聚合POM,它包含所有子模块,一个继承POM,它包含子模块的所有常用功能,以及一个20个左右的子模块。

我已经通过无休止的在线示例循环,了解如何使其工作。在这些工作中,多模块部分都没有。我可以在子目录(和聚合模块)的目标/站点文件夹中建立单独的站点输出。我还让它进入聚合POM中的目标/暂存文件夹。创建的东西看起来不错。

但是

所有子模块链接都不起作用。

我已尝试在jetty下运行此功能,因为对此问题的一些评论说链接仅在它构建为网站时才起作用 - 但是没有,同样的问题,它无法找到孩子的index.html。

任何人都有这样的例子吗?并且为了避免我撕掉更多的头发(上帝知道它已经在那里运行)我宁愿看到实际工作的POM代码和/或所遵循的阶段来纠正测试和部署。

有人可以帮忙吗?

9 个答案:

答案 0 :(得分:12)

我找到了一个“更简单”的解决方案来配置阶段目标。它将自动聚合$ {project.baseURI} / target / stagging文件夹中每个模块的文档。 诀窍是将其添加到所有子模块的父pom中:

  <distributionManagement>
     <site>
        <id>${project.artifactId}-site</id>
        <url>${project.baseUri}</url>
     </site>
  </distributionManagement>

运行

mvn clean site site:stage 
来自pom aggreator 然后看一下target / stagging文件夹。您将正确链接子模块文档!

我希望它能改善您的网站生成!

答案 1 :(得分:9)

好的,终于搞定了。

将此(仅)添加到父POM,根据需要更改暂存文件夹:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.0</version>
    <configuration>
     <stagingDirectory>C:\temp\stage</stagingDirectory>
     <reportPlugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-project-info-reports-plugin</artifactId>
         <version>2.4</version>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-javadoc-plugin</artifactId>
         <version>2.8</version>
         <configuration></configuration>
         <reportSets>
           <reportSet>
             <id>non-aggregate</id>
             <configuration>
               <!-- Specific configuration for the aggregate report -->
               <sourcepath>${project.build.sourceDirectory}/../generated</sourcepath>
             </configuration>
             <reports>
               <report>javadoc</report>
             </reports>
           </reportSet>
           <reportSet>
             <id>aggregate</id>
             <configuration>
               <!-- Specific configuration for the aggregate report -->
               <sourcepath>${project.build.sourceDirectory}/../generated</sourcepath>
             </configuration>
             <reports>
               <report>aggregate</report>
             </reports>
           </reportSet>
         </reportSets>
       </plugin>
        <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-report-plugin</artifactId>
         <version>2.6</version>
       </plugin>
     </reportPlugins>
    </configuration>
</plugin>

将此添加到父级的分发管理部分:

<site>
  <id>${project.artifactId}-site</id>
  <url>./</url>
</site>

然后运行

mvn site site:stage

这应该部署到带有工作链接的临时/站点。

答案 2 :(得分:6)

自上次解决方案以来已经过去了一年多。

我不喜欢这种解决方法,必须有另一种解决方案“maven方式”。

所以这是:

在Maven网站插件常见问题中: http://maven.apache.org/plugins/maven-site-plugin/faq.html#Use_of_url

“另一方面,在多模块构建中使用它来构建相对链接[...]。在多模块构建中,父模块和子模块具有不同的URL非常重要。”

您必须声明&lt; distributionManagement&gt;使用不同的URL在每个pom.xml中标记:

父POM:

<distributionManagement>
  <site>
    <id>mysite</id>
    <name>My Site</name>
    <url>ftp://server.example.com/htdocs/site/</url>
  </site>
</distributionManagement>

Child One POM:

<distributionManagement>
  <site>
    <id>mysite</id>
    <name>My Site</name>
    <url>ftp://server.example.com/htdocs/site/one/</url>
  </site>
</distributionManagement>

Child Two POM:

<distributionManagement>
  <site>
    <id>mysite</id>
    <name>My Site</name>
    <url>ftp://server.example.com/htdocs/site/two/</url>
  </site>
</distributionManagement>

现在,网站的生成和分段按要求工作。已暂存的网站在parent/target/staging

中生成

您可以使用-D

提交另一个暂存目录
mvn -DstagingDirectory=D:/Temp/Site package site site:stage

注意:目标是必需的,如果子二有一个孩子为依赖。使用 package ,执行目标 site ,而不会出现存储库中缺少依赖项的错误。

修改:必须提供&lt; url&gt;对于使用与&lt; distributionManagement&gt;中相同的pathes的每个工件。这是因为使用report-info-plugin生成的index.html使用&lt; url&gt;计算相对路径,但site:stage使用&lt; distributionManagement&gt;。

答案 3 :(得分:4)

子链接在您当前的项目中不起作用,您需要执行site:stage否则它将无效。您需要定义 stagingDirectory ,它定义了必须是绝对路径的目标位置。

答案 4 :(得分:3)

这并不能完全按照你的想法行事。这在Why don't the links between parent and child modules work when I run "mvn site"?常见问题解答中进行了描述。

我不是Maven的专家,但这就是我在本地生成网站的方式:

  1. 在根POM中配置<site>     

    <distributionManagement>
        <site>
            <id>internal.repo</id>
            <name>Temporary Staging Repository</name>
            <url>file://${project.build.directory}/mvn-repo</url>
        </site>
    </distributionManagement>
    
  2. 为您的项目构建网站

    mvn site
    
  3. 生成local staging site。此&#34;根据POM部分中指定的站点URL将生成的站点部署到本地登台或模拟目录。&#34;

    mvn site:stage
    
  4. 默认情况下,该网站将在${project.build.directory}/staging目录下生成

答案 5 :(得分:2)

丢失的一些指示。运行mvn site site:stage后,site目录将不包含组成的站点,该站点将位于staging目录中。根据{{​​3}},在部署站点之前不会完成组合。暂存后,您可以调用mvn site:deploy将组合的站点复制到您在distributionManagement中定义的URL中指定的位置

<distributionManagement>
  <site>
    <id>website</id>
    <url>file://${project.build.directory}/completesite</url>
  </site>
</distributionManagement> 

在这种情况下,完成的站点将最终位于项目构建目录(通常是completesite目录)的目录target中。

答案 6 :(得分:1)

我有一个与OP类似的情况,有一个扭曲:其中一个子模块不从多模块项目父项继承。我想生成一个让所有链接都正常工作的分阶段网站。这是项目结构:

extraParent     // parent not part of this multi-module build
foo
  pom.xml       // aggregator
  fooParent     // parent for this project
  module1       // inherits from fooParent
  extraModule   // inherits from extraParent

@Stefan的回答是朝着正确方向迈出的一大步。特别是,我确认子模块必须重复distributionManagementurl元素。我在fooParent中试过这个:

<properties>
  <foo.site.url>file://somePath/foo/${project.artifactId}</foo.site.url>
</properties>

<url>${foo.site.url}</url>
<distributionManagement>
    <site>
        <id>site-docs</id>
        <url>${foo.site.url}</url>
    </site>
</distributionManagement>

Maven分别计算了fooParent和module1中foo.site.url的正确值:file://somePath/foo/fooParentfile://somePath/foo/module1。但是,顶级站点不包含指向module1的正确链接,除非在子级中重复完全相同的url / distributionManagement块。看起来网站生成正在检查继承的配置而不考虑实际的URL值(即使它们在我查看有效POM时描述的情况不同)。我发现这非常不直观。

我还需要一个配置元素才能让顶层网站在extraModule中正确链接:topSiteURL。我将以下内容添加到extraModule的POM中。注意我刚刚将URL的基本部分(包括顶级目录)提取到topSiteURL值中,然后在foo.site.url属性中使用它。

<properties>
  <topSiteURL>file://somePath/foo/</topSiteURL>
  <foo.site.url>${topSiteURL}/${project.artifactId}</foo.site.url>
</properties>

<url>${foo.site.url}</url>
<distributionManagement>
    <site>
        <id>site-docs</id>
        <url>${foo.site.url}</url>
    </site>
</distributionManagement>

现在,当我使用mvn site site:stage为多模块项目生成网站时,所有链接都有效。

答案 7 :(得分:0)

http://wiki.bitplan.com/index.php/Multi-Module_Maven_with_github_pages,您将在github-pages的上下文中找到详细的分析和示例。另请参见Multi-module example of using mvn site-deploy with github pages

解决方法是什么?

  • 请勿使用插件!
  • git克隆gh页或将其拉到本地文件夹
  • MVN网站:暂存到临时暂存目录
  • 将结果同步到git本地文件夹
  • 检入本地文件夹
  • 喝咖啡或喝啤酒/开心

    请勿使用插件!

  • 它很慢(如示例项目,实际上很慢……18分钟)

  • 不可靠(如500 HTML 代码)

  • 维护得不好(如 截至[https://github.com/github/maven-plugins/issues的57个未解决问题 2018-08]

  • 这是多余的(见下文)即使提交者也没有使用 再也没有了(就像我今天收到的一封个人邮件中的..)
#
# createSite
#   ws: global variable pointing to workspace 
#   param 1: l_project - project name/directory
#   param 2: l_ghpages - directory where gh-pages branch has been git cloned/pulled
#   param 3: l_modules - non-empty for a multi-module project (e.g. containing the list of modules)
#
createSite() {
  local l_project="$1"
  local l_ghpages="$2"
  local l_modules="$3"

  color_msg $green "creating site for $l_project $l_modules"
  cd $ws/$l_project
    stage=/tmp/stage$$
    sitelog=/tmp/sitelog$$.txt
    rm -rf $stage
    # the stagingDirectory needs to be subdirectory 
    mkdir -p $stage/$l_project

    # run the staging of the site against this directory and log the results
    mvn -U clean install site site:stage -DstagingDirectory=$stage/$l_project | tee $sitelog

    # rsync the result into the github-pages folder
    rsync -avz --del $stage/* $l_ghpages/$l_project/

    # is this a multi module project?
    if [ "$l_modules" != "" ]
    then
        cd $l_ghpages/$l_project/
        if [ ! -f index.html ]
        then
cat << EOF > index.html
<!DOCTYPE html>
<html>
<head>
   <!-- HTML meta refresh URL redirection -->
   <meta http-equiv="refresh"
   content="0; url=./$l_project/$l_project/index.html">
</head>
<body>
   <p>This is a multimodule mvn site click below to get to the index.html of 
   <a href="./$l_project/$l_project/index.html">$l_project</a></p>
</body>
</html> 
EOF
        fi
        # add potentially new files
        git add *
        # commit results
        git commit -m "checked in by checksite script"
        # push results
        git push
    fi
    if [ "$debug" = "false" ]
    then
      rm -rf $stage
        rm $sitelog
    fi
}

答案 8 :(得分:0)

我的情况是将以下部分添加到顶部pom中来解决问题:

<distributionManagement>
  <site>
    <id>struts-site</id>
    <url>https://struts.apache.org/maven/</url>
  </site>
</distributionManagement>

现在使用以下命令,它将在target/staging下生成整个页面:

./mvnw clean site site:stage

我正在使用这样的插件配置:

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.0</version>
<configuration>
  <relativizeDecorationLinks>false</relativizeDecorationLinks>
</configuration>

请注意,还必须为每个子模块定义site.xml,因为不继承菜单。

相关问题