简体   繁体   English

如何使用antrun插件将类作为jar文件通过Maven运行

[英]How to run a class as a jar file trough maven with antrun plugin

I have the following plugin configuration in my maven pom.xml 我的Maven pom.xml中有以下插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
        <id>prepare-database</id>
        <phase>pre-integration-test</phase>
        <configuration>
            <target name="run">
                <java fork="false"  failonerror="yes" classname="Test">
                    <arg line="arg1 arg2"/>
                </java>
            </target>
        </configuration>
        <goals>
            <goal>run</goal>
        </goals>
        </execution>
    </executions>
</plugin>

The class is located in a package under src/main/java the package is called com.ab and the class file is called Test.java 该类位于src / main / java下的包中,该包称为com.ab,而类文件称为Test.java

package com.a.b;

public class Test {
    public static void main(String[] args)
      {
        String arg1 = args[0];
        String arg2 = args[1];
        doStuff(arg1, arg2);
      }

    public static void doStuff(String arg1, String arg2)
      {
          System.out.println(arg1);
      }
}

When trying to execute the maven build that runs the ant task I get the following error: 尝试执行运行ant任务的Maven构建时,出现以下错误:

An Ant BuildException has occured: Could not find Test. 发生Ant BuildException:找不到测试。 Make sure you have it in your classpath 确保在类路径中有它

I understand that the file can not be found but I have no clue what to do about it. 我了解找不到该文件,但是我不知道该怎么做。 Any help would be highly appreciated. 任何帮助将不胜感激。

Take a look at the standard-directory-layout article on the Maven site. 看一下Maven网站上的standard-directory-layout文章。

Source folder should be src/main/java (and not src/java/main) 源文件夹应该是src / main / java(而不是src / java / main)

Also, what exactly do you want to do with this ant target ? 另外,您到底想对这个蚂蚁目标做什么? Do you simply need the fork ? 您只需要叉子吗? Why can't you run it as a pure unit test, or a maven integration test and forget about the ANT target ? 为什么不能将其作为纯单元测试或Maven集成测试运行,而忘记了ANT目标?

H2 is well-suited for running in-memory. H2非常适合在内存中运行。 It only takes a couple of lines to connect to the DB and start testing, all inside the JVM, all using standard jUnit code. 只需几行就可以连接到DB并开始测试,所有这些都在JVM内,并且全部使用标准jUnit代码。

 Class.forName("org.h2.Driver"); 
 Connection con = DriverManager.getConnection("jdbc:h2:mem:mytest", "sa", ""); 
 Statement sst = con.createStatement(); 
 sst.executeUpdate(SQL); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行maven-antrun-plugin目标? - how to run maven-antrun-plugin targets? Maven:antrun插件构建JAR文件 - &gt;如何在我的存储库中安装/部署它? - Maven: antrun plugin builds JAR file -> How can i install/deploy it in my repository? Maven:如何使用故障安全插件在jar中运行/包含类? - Maven: How can I run/include a class in a jar with the failsafe plugin? 如何使用surefire插件运行maven jar文件? - How to run maven jar file using surefire plugin? 在Maven构建中对Testng进行套装测试之前运行antrun插件 - Run antrun plugin before suit tests of testng in maven build 如何在Maven目标目录的JAR文件中运行Java类? - How to run java class in JAR file in maven target directory? Spring Boot Maven plugin - How do I build a zip file with Java jar file and script to run it? - Spring Boot Maven plugin - How do I build a zip file with Java jar file and script to run it? Maven,使用antrun复制文件时出错 - Maven, error copying a file with antrun 如何使用maven生成jar文件并运行它? - how to use maven to generated jar file and run it? Maven-如何将代码编译为jar文件而不是.class - maven - how to compile code to jar file and not .class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM