3

配置: Maven 3、Objectify、Google-Appengine、Java 7

我想找到一种datastore-indexes-auto.xml仅使用单元测试自动生成索引(在文件中)的方法。但是,尽管测试调用数据存储查询,但不会生成文件。

在我的项目中,我使用标准的 Maven 布局:

src/
    main/
    test/
target/

使用 Maven 运行我的单元测试时,不会datastore-indexes-auto.xml生成任何文件(至少我在项目目录中的任何地方都找不到它)。当然,所有测试都通过了。

有没有办法只使用单元测试自动生成索引?

这是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.something.example</groupId>
  <artifactId>example</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Example</name>
  <description>Example POM for my appengine project.</description>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gae.version>1.8.1</gae.version>
    <objectify.version>4.0rc1</objectify.version>
    <java.version>1.7</java.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${gae.version}</version>
        <configuration>
          <oauth2>false</oauth2>
          <address>0.0.0.0</address>
        </configuration>
      </plugin>
      <!-- UPDATE: Based on @Amir's answer, I have added this plugin in order to
        -- copy the generated index file to the webapp directory. -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>test</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/${project.artifactId}-${project.version}/WEB-INF/appengine-generated</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/WEB-INF/appengine-generated</directory>
                  <filtering>false</filtering>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- UPDATE: This will remove the genreated WEB-INF directory with the mvn clean 
        -- command. -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <filesets>
            <fileset>
              <directory>WEB-INF</directory>
            </fileset>
          </filesets>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>0.11.8</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.googlecode.objectify</groupId>
      <artifactId>objectify</artifactId>
      <version>${objectify.version}</version>
    </dependency>
    <!-- GAE -->
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-1.0-sdk</artifactId>
      <version>${gae.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-labs</artifactId>
      <version>${gae.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-stubs</artifactId>
      <version>${gae.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-testing</artifactId>
      <version>${gae.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>httpunit</groupId>
      <artifactId>httpunit</artifactId>
      <version>1.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mozilla</groupId>
      <artifactId>rhino</artifactId>
      <version>1.7R4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.5</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
4

2 回答 2

2

我让它以一种方式工作。这有点hacky,但它似乎确实有效。

请注意,使这项工作与 Maven 几乎没有关系。关键是配置本地数据存储服务生成datastore-indexes-auto.xml。

第 1 步:将本地数据存储服务测试配置修改为 setNoIndexAutoGen(false)。此外,不幸的是,如果您希望“datastore-indexes-auto.xml”包含测试所需的所有索引,则必须注释掉 LocalServiceTestHelper tearDown() 调用。

请注意,我的所有数据存储测试都继承自 PersistenceTestHelper。所以这就是我改变它的方式:

public class PersistenceTestHelper {

  private LocalServiceTestHelper appEngineHelper;

  @Before
  public void setUp() throws Exception {
    LocalDatastoreServiceTestConfig dsConfig = new LocalDatastoreServiceTestConfig();
    dsConfig.setNoIndexAutoGen(false);
    appEngineHelper = new LocalServiceTestHelper(dsConfig);
    appEngineHelper.setUp();
    ...
  }

  @After
  public void tearDown() throws Exception {
    // appEngineHelper.tearDown();
  }
}

第 2 步:执行“mvn 测试”

第 3 步:在您执行“mvn test”的目录中查找 WEB-INF 子目录。在那里您会找到“WEB-INF/appengine-generated/datastore-indexes-auto.xml”。这是项目目录/pom.xml 独立的。

$ pwd
/home/avaliani/src/karma-exchange
$ mvn test
...
$ cat WEB-INF/appengine-generated/datastore-indexes-auto.xml 
<!-- Indices written at Tue, 2 Jul 2013 03:52:26 UTC -->

<datastore-indexes>

    <!-- Used 1 time in query history -->
    <datastore-index kind="Review" ancestor="true" source="auto">
        <property name="+commentCreationDate" direction="asc"/>
    </datastore-index>

    <!-- Used 1 time in query history -->
    <datastore-index kind="Review" ancestor="true" source="auto">
        <property name="commentCreationDate" direction="desc"/>
    </datastore-index>

</datastore-indexes>

详细信息: 我正在使用“Apache Maven 3.0.4”和 App Engine 1.7.7 SDK。我的项目与 App Engine 的 maven 插件集成:https ://developers.google.com/appengine/docs/java/tools/maven

我的 pom.xml: https ://github.com/karma-exchange-org/karma-exchange/blob/master/pom.xml

于 2013-07-02T03:55:16.693 回答
0

您是否尝试过将datastore-indexes.xml文件设置为以下形式:

<?xml version="1.0" encoding="UTF-8"?>
<datastore-indexes autoGenerate="true">
</datastore-indexes>
于 2013-07-02T08:52:15.260 回答