Maven 如何在过滤后预编译文件?

Maven 如何在过滤后预编译文件?,maven,Maven,我正在构建一个.war文件,其中包含一个.jsp文件。jsp中有一些maven属性变量,因此我需要使用maven war插件对其进行过滤。 除此之外,我还想使用MojoJSPC插件对其进行预编译,并将.class文件打包到war文件中 但是,未编译过滤后的文件。结果war文件由过滤的.jsp文件和非过滤的.class文件组成 如何配置pom.xml以编译过滤后的.jsp 我的pom文件如下所示: <build> <finalName>${Component}</

我正在构建一个.war文件,其中包含一个.jsp文件。jsp中有一些maven属性变量,因此我需要使用maven war插件对其进行过滤。 除此之外,我还想使用MojoJSPC插件对其进行预编译,并将.class文件打包到war文件中

但是,未编译过滤后的文件。结果war文件由过滤的.jsp文件和非过滤的.class文件组成

如何配置pom.xml以编译过滤后的.jsp

我的pom文件如下所示:

  <build>
<finalName>${Component}</finalName>
<resources>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>
<plugins> 
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <archiveClasses>true</archiveClasses>  
      <attachClasses>true</attachClasses> 
      <packagingExcludes>**/*.vpp,WEB-INF/web.xml,**/eclipselink.jar,**/coherence.jar,**  /toplink-grid.jar</packagingExcludes>
      <outputFileNameMapping>@{artifactId}@.@{extension}@</outputFileNameMapping> 
      <webResources>
        <resource>
          <directory>src/main/webapp/verification</directory>
          <targetPath>verification</targetPath> 
          <filtering>true</filtering> 
        </resource>
      </webResources>  
      <executions>
          <execution>
              <phase>process-resources</phase> 
          </execution>
      </executions>         
    </configuration>
  </plugin>



  <!-- start jspc -->

  <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>jspc-maven-plugin</artifactId>
     <executions>
         <execution>
             <id>jspc</id>           
             <goals>        
                 <goal>compile</goal>
             </goals>             
              <phase>compile</phase> 
         </execution>
     </executions>
 </plugin>
  <!-- end jspc -->

</plugins>
</build>

JSP文件存储在src/main/webapp/verification中,很可能插件的执行顺序错误。大多数插件允许通过添加例如生成资源来更改绑定到的阶段。您需要显式地将插件绑定到阶段,以便首先执行过滤。各阶段都有文件记录。选择要绑定到的适当对象。 顺便说一句,直接过滤JSP是一种非常糟糕的做法。相反,生成一个.properties文件来过滤它,或者动态生成并将其加载到Servlet上下文中,这样所有JSP都可以使用它。如果你使用春天,这是微风。下面的示例runtime.properties是过滤/生成的文件:

<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
        <property name="attributes">
            <map>
                <entry key="config">
                    <util:properties location="classpath:runtime.properties"/>
                </entry>
            </map>
        </property>
    </bean>

然后在JSP中以config['someKey']的形式访问它。这也消除了对jsp被过滤阶段的担忧。

或者我可以引导jspc插件在目标文件中预编译过滤后的jsp吗?是的,这可能也会起作用。目标只是在正确的文件上以正确的顺序执行这两个步骤。不过,我仍然认为最好的选择是根本不直接过滤JSP。