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

一方向に動作するようにしました。少しハックですが、うまくいくようです。

これを機能させることは、maven とはほとんど関係がないことに注意してください。重要なのは、datastore-indexes-auto.xml を生成するようにローカル データストア サービスを構成することです。

ステップ 1:ローカル データストア サービスのテスト構成を setNoIndexAutoGen(false) に変更します。また、残念ながら、 「datastore-indexes-auto.xml」にテストに必要なすべてのインデックスを含める場合は、LocalServiceTestHelper の TeaDown() 呼び出しをコメントアウトする必要があります。

すべてのデータストア テストは 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 test」を実行する

ステップ 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 に答える