我在这个ant trycatch块中缺少什么?

时间:2014-10-01 15:53:01

标签: ant try-catch ant-contrib

我正在尝试在我的ant脚本中创建trycatch块。我收到这个错误:

*更新*

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (test-compile) on project PROJECT: An Ant BuildException has occured: Problem: failed to create task or type trycatch
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

around Ant part ...<trycatch>... @ 6:13 in /Users/NAME/Projects/PROJECT-dev/PROJECT-website/target/antrun/build-main.xml
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: An Ant BuildException has occured: Problem: failed to create task or type trycatch
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

around Ant part ...<trycatch>... @ 6:13 in /Users/NAME/Projects/PROJECT-dev/PROJECT-website/target/antrun/build-main.xml
    at org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:355)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 19 more
Caused by: /Users/NAME/Projects/PROJECT-dev/PROJECT-website/target/antrun/build-main.xml:6: Problem: failed to create task or type trycatch
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

    at org.apache.tools.ant.UnknownElement.getNotFoundException(UnknownElement.java:487)
    at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:419)
    at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
    at org.apache.tools.ant.Task.perform(Task.java:347)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:327)
    ... 21 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我尝试按照try catch的例子,但没有一个工作。 http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html

我收到此错误在所有情况下都没有定义同名。我不知道我是否将这个块嵌套在正确的位置,因为我试图将它放在特定的构建阶段。

如何让这个trycatch工作?

*更新*

我仍然遇到同样的错误

<dependencies>
...
    <dependency>
        <groupId>ant-contrib</groupId>
        <artifactId>ant-contrib</artifactId>
        <version>1.0b3</version>
    </dependency>
...
</dependencies>

...

            <execution>
                <id>test-compile</id>
                <phase>test-compile</phase>
                <configuration>
                 <tasks>
                      <!-- declare ant-contrib tasks -->
                      <taskdef resource="net/sf/antcontrib/antlib.xml" />
                      <echo message="***** Hello from test-compile phase! *****" />
                      <trycatch>
                          <try>
                              <replaceregexp match="\s+" replace=" " flags="g" byline="true">
                                  <fileset dir="${basedir}/target/ROOT/WEB-INF/views/" includes="**/*.jsp"></fileset>
                              </replaceregexp>
                              <replaceregexp match="&lt;!--.*build.*--&gt;" replace=" " flags="g" byline="true">
                                  <fileset dir="${basedir}/target/ROOT/WEB-INF/views/" includes="**/*.jsp"></fileset>
                              </replaceregexp>
                          </try>
                      </trycatch>
                  </tasks>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>

1 个答案:

答案 0 :(得分:0)

trycatchant-contrib的一部分。因此,您需要在此库中声明插件的依赖项,并在执行配置中的任务之前声明其中包含的任务:

<execution>
    <id>test-compile</id>
    <phase>test-compile</phase>
    <configuration>
         <tasks>
              <!-- declare ant-contrib tasks -->
              <taskdef resource="net/sf/antcontrib/antlib.xml" />
              <echo message="***** Hello from test-compile phase! *****" />
              <trycatch>
                  <try>
                      <replaceregexp match="\s+" replace=" " flags="g" byline="true">
                          <fileset dir="${basedir}/target/ROOT/WEB-INF/views/" includes="**/*.jsp"></fileset>
                      </replaceregexp>
                      <replaceregexp match="&lt;!--.*build.*--&gt;" replace=" " flags="g" byline="true">
                          <fileset dir="${basedir}/target/ROOT/WEB-INF/views/" includes="**/*.jsp"></fileset>
                      </replaceregexp>
                  </try>
              </trycatch>
          </tasks>
      </configuration>
      <goals>
          <goal>run</goal>
      </goals>
</execution>
<dependencies>
      <dependency>
          <groupId>ant-contrib</groupId>
          <artifactId>ant-contrib</artifactId>
          <version>1.0b3</version>
      </dependency>
</dependencies>