Macaca ubuntu16.04 Eclipse 使用 Maven pom 和 settings 创建本地 macaca java 项目记

老马 · 2016年08月26日 · 最后由 曾晖斌 回复于 2017年03月24日 · 1975 次阅读

一 配置好 Eclipse 及其相关的 maven 插件和 git 插件等和 android 开发环境

当然还有 ubuntu 环境下的 macaca-cli 和各种驱动 等 macaca 执行环境,请参考ubuntu16.04 环境折腾 macaca 总结记
此处省略,自行度娘 谷哥.许久之前就配置好了,这次是直接拿来用的.

二 Eclipse 创建新 Maven 项目

在之前的ubuntu16.04 Eclipse 尝试 Macaca Java 记
ubuntu16.04 Eclipse Macaca Java 测试 Android APP 记
细心的观众可能发现我加了备注 "如果你熟悉 maven 的 pom 和 settings 设置 也完全可以创建维护个人的 macaca java 项目 " 那本次实际就是解决这个问题.

Group Id : com.test.macaca
Artifact id : UiAutoTest
Package : uiauto

初始项目结构和 pom 如下

三 修改 pom 文件
参考 https://github.com/macacajs/macaca-test-sample-java/blob/master/pom.xml

<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>com.test.macaca</groupId>
  <artifactId>UiAutoTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>UiAutoTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

      <dependencies>
        <dependency>
            <groupId>macaca.webdriver.client</groupId>
            <artifactId>macacaclient</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

四 创建 settings.xml 文件

这个需要自己去研究熟悉 maven 的 settings.xml 配置写法.
内容如下:

先找到本地 maven settings.xml 位置

cmd@TR:~$ mvn -v
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_101, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-34-generic", arch: "amd64", family: "unix"
cmd@TR:~$ cd /usr/share/maven/conf
cmd@TR:/usr/share/maven/conf$ ls -al
总用量 40
drwxr-xr-x   3 root root  4096 8月  29 11:40 .
drwxr-xr-x 150 root root 12288 8月  29 08:12 ..
drwxr-xr-x   2 root root  4096 8月  19 11:25 logging
-rw-r--r--   1 root root   222 11月 19  2015 m2.conf
-rw-r--r--   1 root root 10809 8月  29 11:40 settings.xml
-rw-r--r--   1 root root  3649 11月 19  2015 toolchains.xml
cmd@TR:/usr/share/maven/conf$ 

可以将该/usr/share/maven/conf/settings.xml 先 cp 到 /home/cmd/.m2/ 下,然后编辑修改
是官方 maven 标准 settings.xml http://maven.apache.org/settings.html#和参考 https://github.com/macacajs/wd.java/blob/master/settings.xml
实际有用的 /home/cmd/.m2/repository 节 和 ** **节等.其他都被注释掉了,您根据自己项目部署需要按需配置.

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>/home/cmd/.m2/repository</localRepository>

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
   <profile>
            <id>bintray</id>
            <repositories>
                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>bintray</name>
                    <url>http://jcenter.bintray.com</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>bintray-plugins</name>
                    <url>http://jcenter.bintray.com</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>  
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
  <activeProfiles>
        <activeProfile>bintray</activeProfile>
    </activeProfiles>
</settings>

Eclipse->windows->Preferences

可以将该 settings 复制到~/.m2/settings.xml 这样的好处是,请参考:
默认情况下,~/.m2 目录下除了 repository 仓库之外就没有其他目录和文件了,不过大多数 Maven 用户需要复制 M2_HOME/conf/settings.xml 文件到~/.m2/settings.xml。这是一条最佳实践,我们将在本章最后一小节详细解释。
配置用户范围 settings.xml
Maven 用户可以选择配置 $M2_HOME/conf/settings.xml 或者~/.m2/settings.xml。前者是全局范围的,整台机器上的所有用户都会直接受到该配置的影响,而后者是用户范围的,只有当前用户才会受到该配置的影响。
我们推荐使用用户范围的 settings.xml,主要原因是为了避免无意识地影响到系统中的其他用户。当然,如果你有切实的需求,需要统一系统中所有用户的 settings.xml 配置,当然应该使用全局范围的 settings.xml。
除了影响范围这一因素,配置用户范围 settings.xml 文件还便于 Maven 升级。直接修改 conf 目录下的 settings.xml 会导致 Maven 升级不便,每次升级到新版本的 Maven,都需要复制 settings.xml 文件,如果使用~/.m2 目录下的 settings.xml,就不会影响到 Maven 安装文件,升级时就不需要触动 settings.xml 文件。

最后 Eclipse 中把官方https://github.com/macacajs/macaca-test-sample-java/tree/master/src/test/java/macaca/client
SampleTest.java 复制过来,然后选中项目 UiautoTest 右键 Run as->Mvn test 最后看到 BUILD SUCCESS 说明整个执行环境测试正常。

也可以利用 mvn 命令行到项目目录
cmd@TR:~/workspace/MacacaDev/uiautotest$ mvn test
下去执行 mvn test 效果和上述在 Eclipse 里右键 mvn test 实际是一样的效果。

五 新建自己的 macaca Java 测试类

剩下的参考win10 环境 Eclipse macaca java 使用记的四 五节
和之前我的 wiki 系列即可 小马的 macaca 入门指引合集

cmd@TR:~/workspace/MacacaDev/uiautotest$ mvn test -Dtest=uiauto.H5Mobile

更多信息请参考 wiki 汇总

小马的 macaca 入门指引合集

更多的关于 wd.java 样例学习可参考 wd.java 的作者的样例:

https://github.com/Yinxl/bootstrap
这个例子很好 common 里 提取了一些公共 可复用部分

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 9 条回复 时间 点赞
老马 [该话题已被删除] 中提及了此贴 08月29日 15:54
老马 [该话题已被删除] 中提及了此贴 08月29日 15:55
老马 [该话题已被删除] 中提及了此贴 09月22日 14:44
老马 ubuntu16.04 Eclipse 尝试 Macaca Java 记 中提及了此贴 11月21日 10:46
老马 ubuntu16.04 Eclipse Macaca Java 测试 Android APP 记 中提及了此贴 11月21日 10:48

我在 pom 文件中添加 macacaclient 依赖时总是提示:Missing artifact macaca.webdriver.client:macacaclient:jar:
1.0.2,请问时什么原因呢?

#8 楼 @miumiu 我也碰到了。2.0.1 。你怎么解决的

maven pom 文件一直提示个错误:Missing artifact macaca.webdriver.client:macacaclient:jar:2.0.1 怎么解决的呢?谢谢大神了。@harsayer ayer @junhe @xdf

@ht 刚搞这个,和你遇到问题一样,你解决了没?

12楼 已删除
老马 #13 · 2017年02月20日 Author

#11 楼 @success 参考 wiki 答案都有 1.0.2 都老早的版本了 请参考 wiki 里边有让你去参考 https://github.com/macacajs
里边去找各 macaca 依赖组件版本 最新的 wd.java 也就是 pom 里的 macacaclient 是这个https://github.com/macacajs/wd.java 2.0.1 了

#9 楼 @ht 我还没解决完成😂

我也遇到了同样的问题,各位都解决了没有呀@ht @success @miumiu

@haihanty
配置 maven 的 setting.xml

  <mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>        
  </mirror>
</mirrors>

macaca 在 maven 默认的仓库下架了,所以会提示找不到
上面是配置阿里的 maven 镜像,就可以下载成功了

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册