java - 无法将参数从命令行传递到 Maven 项目。我想传递可以运行测试的服务器 URL

标签 java eclipse maven

我是第一次创建 Maven 项目,之前我可以使用 mvn test 命令在不传递参数的情况下运行它。
现在我想将参数传递给它,所以我更改了 pom.xml 但出现以下错误

BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.209 s
[INFO] Finished at: 2014-12-23T12:52:44+05:30
[INFO] Final Memory: 5M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Could not find goal 'exec' in plugin org.apache.maven.plugins:maven-compiler-plugin:2.3.2 among available goals help, compile, testCompile -> [Help 1]
org.apache.maven.plugin.MojoNotFoundException: Could not find goal 'exec' in plugin org.apache.maven.plugins:maven-compiler-plugin:2.3.2 among available goals help, compile, testCompile
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getMojoDescriptor(DefaultMavenPluginManager.java:273)
    at org.apache.maven.plugin.DefaultBuildPluginManager.getMojoDescriptor(DefaultBuildPluginManager.java:239)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.getMojoDescriptor(MojoDescriptorCreator.java:233)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:103)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:83)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:85)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
    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)
[ERROR]
[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/MojoNotFoundException

这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/3.2.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/3.2.3 http://maven.apache.org/xsd/maven-3.2.3.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Grok</groupId>
  <artifactId>MobileApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Login to app</name>
  <description>Login to Grok app</description>

  <repositories>
    <repository>
        <id>saucelabs-repository</id>
        <url>https://repository-saucelabs.forge.cloudbees.com/release</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

  <dependencies>
  <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.44.0</version>
    </dependency>  
    <dependency>
     <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
     <groupId>com.saucelabs</groupId>
     <artifactId>sauce_junit</artifactId>
     <version>2.1.3</version>
     <scope>test</scope>
    </dependency>
  </dependencies>

   <build>
   <pluginManagement>
        <plugins>
            <!-- Include Sauce Connect plugin -->
            <plugin>
                <groupId>com.saucelabs.maven.plugin</groupId>
                <artifactId>sauce-connect-plugin</artifactId>
                <version>2.1.9</version>
                <configuration>
                    <sauceUsername>admins-test-numenta</sauceUsername>
                    <sauceAccessKey>f6c91fa5-d400-42be-85e1-f7060098f922</sauceAccessKey>
                    <options>-i testing</options>
                </configuration>
                <executions>
                    <execution>
                        <id>start-sauceconnect</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-sauceconnect</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-sauceconnect</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop-sauceconnect</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>exec-one</id>
                        <phase>verify</phase>
                        <configuration>
                            <executable>echo</executable>
                            <variables>
                          <variable>serverUrl:${Gvars.server}</variable>
                        </variables>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <executions>
                <execution>
                    <configuration>
                         <variables>
                              <variable>serverUrl:${RFvars.browser}</variable>
                            </variables>
                    </configuration>

                     <goals>
                                <goal>exec</goal>
                            </goals>
                            </execution>
                            </executions>
                </plugin>
            </plugins> 
           </pluginManagement> 
    </build>         
</project>

我从命令行运行它

mvn org.apache.maven.plugins:maven-compiler-plugin:exec -DGvars.server=url_to_be_used

如果我在 pom 文件或运行项目的命令中做错了什么,请告诉我。 提前致谢

最佳答案

问题出在你的 maven-compiler-plugin 上配置。由于错误消息指出编译器插件没有 exec 目标(Could not find goal 'exec' in plugin org.apache.maven.plugins:maven-compiler-plugin)。

它只有两个可用目标,它们是:

  • 编译
  • 测试编译

此外,我不确定您希望编译器插件如何处理变量配置。它没有know关于变量,因此将忽略它​​。

将编译器插件配置更改如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <executions>
        <execution>
           <goals>
               <goal>compile</goal>
           </goals>
        </execution>
    </executions>
</plugin>

但实际上这是默认配置,因此您可以进一步简化它:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
</plugin>

关于java - 无法将参数从命令行传递到 Maven 项目。我想传递可以运行测试的服务器 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27615889/

相关文章:

java - 用于选择 rtf 源的特定部分的正则表达式

java - 如何重写实现接口(interface)的类中的方法

eclipse - Tomcat 服务器在 45 秒内未启动

java - 无法使用 Spring 访问 Maven 属性

jsp - JSTL 中的资源包显示问号?

java - 你如何制作一个可变的类级别?

c++ - 在eclipse : how?中使用googletest

JavaFX:如何显示表情符号?

spring - 如何解决 codehaus "service unavailable"maven 构建错误?

java - 使用 4 个选项制作 JOptionPane