如何在pom.xml文件中声明用户自定义属性

huangapple go评论77阅读模式
英文:

How to declare user defined properties in pom.xml file

问题

我正在使用Maven项目进行UI自动化测试,并需要在多个环境和多个浏览器中运行一些测试。
我已经创建了pom.xml文件并在其中声明了属性。但是当我通过终端执行时,出现了错误。
我使用的命令是-

mvn clean

mvn test -Denvironment=test -Dbrowser=chrome

我的XML文件-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>ui-automation-projects</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <property>
            <environment>${environment}</environment>
        </property>
        <property>
            <browser>${browser}</browser>
        </property>
    </properties>


    <dependencies> </dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>

错误信息-
[INFO] 正在扫描项目...
[ERROR] [ERROR] 处理POM文件时出现一些问题:
[FATAL] 无法解析的POM D:\work\Cucumber_Projects\ui-automation-projects\pom.xml:TEXT必须紧接着END_TAG而不是START_TAG(位置:START_TAG seen ...\n ... @13:26)@ line
13, column 26

英文:

I am using maven project for UI automation and I need to run some tests in multiple environment and multiple browsers.
I have created added the pom.xml file and declared the properties in it. But when i execute it through the terminal i get errors.
command i used-

mvn clean

mvn test -Denvironment=test -Dbrowser=chrome

My Xml file-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;ui-automation-projects&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;property&gt;
            &lt;environment&gt;${environment}&lt;/environment&gt;
        &lt;/property&gt;
        &lt;property&gt;
            &lt;browser&gt;${browser}&lt;/browser&gt;&gt;
        &lt;/property&gt;
    &lt;/properties&gt;


    &lt;dependencies&gt; &lt;/dependencies&gt;
&lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.8&lt;/source&gt;
                    &lt;target&gt;1.8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0-M4&lt;/version&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

Error-
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-parseable POM D:\work\Cucumber_Projects\ui-automation-projects\pom.xml: TEXT must be immediately followed by END_TAG and not START_TAG (position: START_TAG seen ...<property>\n <environment>... @13:26) @ line
13, column 26

答案1

得分: 1

在pom.xml中声明项目属性如下:

<properties>
    <environment>默认环境值</environment>
    <browser>默认浏览器值</browser>
</properties>

您可以在POM参考文档中了解更多信息。

英文:

Project properties in pom.xml are declared like this:

&lt;properties&gt;
    &lt;environment&gt;default-environemnt-value&lt;/environment&gt;
    &lt;browser&gt;default-browser-value&lt;/browser&gt;&gt;
&lt;/properties&gt;

You can read more in POM reference

答案2

得分: 1

你必须删除&lt;property&gt;并在&lt;properties&gt;下添加你的属性。

     &lt;environment&gt;${environment}&lt;/environment&gt;
     &lt;browser&gt;${browser}&lt;/browser&gt;
&lt;/properties&gt;

另外,在&lt;/browser&gt;后面多了一个&#39;&gt;&#39;

英文:

You must delete &lt;property&gt; and add your property under &lt;properties&gt;.

&lt;properties&gt;
     &lt;environment&gt;${environment}&lt;/environment&gt;
     &lt;browser&gt;${browser}&lt;/browser&gt;

 &lt;/properties&gt;

Also, you have an extra '>' after &lt;/browser&gt;

答案3

得分: -1

你在解析 pom.xml 文件时出现了错误,我在我的 Idea 中检查并进行了修正。你缺少了 &lt;/project&gt; 标签,而 &lt;dependencies&gt; &lt;/dependencies&gt; 是多余的。此外,应该有以下答案:

&lt;properties&gt;
    &lt;environment&gt;${envValue}&lt;/environment&gt;
    &lt;browser&gt;${browserValue}&lt;/browser&gt;
&lt;/properties&gt;

在添加这些内容后,文件将能够正确构建。

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;ui-automation-projects&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;environment&gt;${envValue}&lt;/environment&gt;
        &lt;browser&gt;${browserValue}&lt;/browser&gt;
    &lt;/properties&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.8&lt;/source&gt;
                    &lt;target&gt;1.8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0-M4&lt;/version&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

这样文件就能正确构建了。

英文:

You have error during the parson pom.xml file I checked it into my Idea and corrected. You didn't have &lt;/project&gt; tag and &lt;dependencies&gt; &lt;/dependencies&gt; were redundant. Also there should be answer

&lt;properties&gt;
    &lt;environment&gt;${envValue}&lt;/environment&gt;
    &lt;browser&gt;${browserValue}&lt;/browser&gt;
&lt;/properties&gt;

After this the file builds correctly.

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;ui-automation-projects&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
      &lt;environment&gt;${envValue}&lt;/environment&gt;
      &lt;browser&gt;${browserValue}&lt;/browser&gt;
    &lt;/properties&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.8&lt;/source&gt;
                    &lt;target&gt;1.8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0-M4&lt;/version&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

答案4

得分: -3

我看到了你的POM几次,也看到了我的本地POM,我是如何定义我的依赖关系和POM的。

我发现了两件事情:

  1. 你已经在属性中定义了可以直接添加值的属性(在你的情况下是environment和Browser),否则可以单独定义如下:
    ${environment}
  2. 我没有在POM中看到项目的结束标记。尝试添加它,应该可以正常工作。
英文:

I have seen u r POM couple of ties and have seen my local pom how I have defined my dependencies and POM
I found 2 things

  1. You have defined the properties already in which u can directly add the value
    (Here in your case it is environment and Browser)
    else define alone as follows in properties
    ${environment}
  2. I haven't seen end for the Project in the POM.
    Try adding that and that should work.

enter image description here

huangapple
  • 本文由 发表于 2020年8月6日 02:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63271315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定