2011年10月17日月曜日

mavenでAIRする

はじめに

今回は、flexmojos-maven-pluginでFlex-AIRアプリを作ろう!というのがテーマです。
このMavenプラグインを使ってうれしいことは
  1. 無償である。
  2. MavenリポジトリにアップされているFlex-SDKを利用できるのでダウンロード、インストールなどが不要である。
などなどです。
それでは、Getting Started!!

環境

OS Windows XP
ビルドツール Maven 3.0.3 ※mvnコマンドが実行できるように設定しておきます。

作業フォルダ作成

とりあえずCドライブ直下にプロジェクトフォルダを作成します。
コマンドプロンプトを開いて以下のコマンドを実行します。
cd c:\
mkdir maven-air
cd maven-air
作成した作業フォルダ作成に移動しておきます。

pom.xmlファイルを作る

コマンドプロンプトから以下のコマンドを実行します。
mvn archetype:generate ^
-DarchetypeRepository=http://repository.sonatype.org/content/groups/flexgroup ^
-DarchetypeGroupId=org.sonatype.flexmojos ^
-DarchetypeArtifactId=flexmojos-archetypes-application ^
-DarchetypeVersion=4.0-beta-3
archetypeVersionについて
現時点での最新と思われる 4.0-beta-3 を指定します。
groupId、artifactId、versionは以下のようにします。

groupId mydomain.com
artifactId my-air
version 1.0-SNAPSHOT

コマンド実行後、プロジェクトフォルダとpomファイルが生成されます。
次に生成されたpomファイルを以下のような感じで編集します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?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/maven-v4_0_0.xsd">
      
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>mydomain.com</groupId>
  <artifactId>my-air</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>swf</packaging>
 
  <name>my-air Flex</name>
 
  <properties>
    <application.name>Main</application.name>
    <flex.framework.version>4.5.0.17855</flex.framework.version>
    <keystore.file>${basedir}/src/main/resources/sign.p12</keystore.file>
    <keystore.password>geheim</keystore.password>
  </properties>
   
  <build>
    <sourceDirectory>src/main/flex</sourceDirectory>
    <testSourceDirectory>src/test/flex</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.sonatype.flexmojos</groupId>
        <artifactId>flexmojos-maven-plugin</artifactId>
        <version>4.0-beta-3</version>
        <extensions>true</extensions>
        <configuration>
          <flexBuilderCompatibility>true</flexBuilderCompatibility>
          <sourceFile>${application.name}.mxml</sourceFile>
          <descriptorTemplate>${basedir}/src/main/flex/${application.name}-app.xml</descriptorTemplate>
          <keystore>${keystore.file}</keystore>
          <storepass>${keystore.password}</storepass>
          <includeFileSets>
            <fileSet>
              <directory>src/main/resources/embedded</directory>
              <includes>
                <include>*.*</include>
              </includes>
            </fileSet>
          </includeFileSets>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>sign-air</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
 
  <dependencies>
    <dependency>
      <groupId>com.adobe.flex.framework</groupId>
      <artifactId>flex-framework</artifactId>
      <version>${flex.framework.version}</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>com.adobe.flexunit</groupId>
      <artifactId>flexunit</artifactId>
      <version>0.85</version>
      <type>swc</type>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.adobe.flex.framework</groupId>
      <artifactId>air-framework</artifactId>
      <version>${flex.framework.version}</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>com.adobe.flex</groupId>
      <artifactId>compiler</artifactId>
      <version>${flex.framework.version}</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>com.adobe.flex.compiler</groupId>
      <artifactId>adt</artifactId>
      <version>${flex.framework.version}</version>
    </dependency>
  </dependencies>
 
  <repositories>
    <repository>
      <id>flexmojos</id>
      <url>http://repository.sonatype.org/content/groups/flexgroup</url>
    </repository>
  </repositories>
 
  <pluginRepositories>
    <pluginRepository>
      <id>flexmojos</id>
      <url>http://repository.sonatype.org/content/groups/flexgroup</url>
    </pluginRepository>
  </pluginRepositories>
  
  <profiles>
    <profile>
      <id>createCertificateKey</id>
      <activation>
        <file>
          <missing>${basedir}/src/main/resources/sign.p12</missing>
        </file>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1.1</version>       
            <executions>
              <execution>
                <id>generate-cert</id>
                <phase>generate-resources</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                  <configuration>
                    <executable>java</executable>
                    <workingDirectory>${basedir}/src/main/resources</workingDirectory>
                    <arguments>
                      <argument>-classpath</argument>
                      <classpath/>
                      <argument>com.adobe.air.ADT</argument>
                      <argument>-certificate</argument>
                      <argument>-cn</argument>
                      <argument>AirPackage</argument>
                      <argument>2048-RSA</argument>
                      <argument>sign.p12</argument>
                      <argument>geheim</argument>
                    </arguments>
                  </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  
</project>
CertificateKeyファイルについて
ビルドの度にCertificateKeyファイルを作ってAIRパッケージするとAIRインストール時にエラーが出る。
なのでprofile設定を利用してCertificateKeyファイルが無い時にだけ生成するようにします。
(同じCertificateKeyファイルを使ってパッケージしないといけないようだ)
sign-airゴールの設定をうまくやればこのprofile設定は不要かもしれません。
わたしはよく分からなかったのでexec-maven-pluginでCertificateKeyファイルを生成する方法にしました。

テンプレートファイル作成

AIRパッケージのためのテンプレートファイル作成します。
ファイル名は 『Main-app.xml』 として、『c:\maven-air\my-air\src\main\flex』 フォルダに置きます。

Main-app.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.5.3">
  <id>com.mydomain.MyApp</id>
  <filename>Main</filename>
  <name>My Application</name>
  <version>1.0</version>
  <initialWindow>
    <content>my-air-1.0-SNAPSHOT.swf</content>
    <visible>true</visible>
    <width>800</width>
    <height>600</height>
  </initialWindow>
</application>

プロジェクト構成の確認

確認の前に embedded フォルダを作成します。
コマンドプロンプトを開いて以下のコマンドを実行します。
  
mkdir C:\maven-air\my-air\src\main\resources\embedded
  
この時点で以下のようなファイル構成になっているか確認します。
  • C:\maven-air
    • my-air
      • src
        • main
          • flex
            • Main.mxml
            • Main-app.xml
          • resources
            • embedded
        • test
          • flex
            • mydomain
              • com
                • TestApp.as
      • pom.xml
sign-airゴールについて
embedded フォルダを作るのを忘れるとビルドの時のsign-airゴール実行に
NullPointerExceptionが出るので気をつけましょう。

ビルド!!

いよいよビルドの瞬間がやって参りました。
うまくいきますかねぇ?
ドキドキしますね!
それでは pom.xml ファイルのあるプロジェクトフォルダに移動してコマンドプロンプトから以下のコマンドを実行!!!
cd c:\maven-air\my-air
mvn clean package -Dmaven.test.skip=true
ここに来て残念なお知らせが・・・
テスト実行時、 Flash Playerか見つからない等のエラーが出てしまいます。
(わたしの環境だけなのかもしれませんが・・・)
PATH変数やmvnコマンドの引数にFlashPlayer.exeを使いしてみたりしたのですがうまくいきませんでした。
なので今回、テストは見送りです。
わたしの設定の仕方が間違っているのか、プラグインがおかしいのかよく分かりません。
まぁオープンソースなのでそんなものです。
いつかソースコードを眺めてみたいと思っています。
コマンド実行後、targetフォルダ直下に my-air-1.0-SNAPSHOT.air ファイルが出力されます。
このファイルをダブルクリックするとAIRインストーラーが起動します。
あとは適当にインスールするフォルダを選択して完了です。
AIRアプリのインストールに失敗する場合
同じAIRアプリを違うCertificateKeyファイルでパッケージするができないようです。
そんな時は、コントロールパネルの『プログラムの追加と削除』から一旦AIRアプリを削除してから
再度my-air-1.0-SNAPSHOT.air ファイルを実行してみよう。

おわりに

とりあえずは、無償でFlex-AIRアプリがそこそこお手軽に作れる環境が整ったとは思いますが
いくつか問題点は残りました。(テストなど)
ここがFlashBuilderを購入するか悩むところです。
仕事ならばFlashBuilder購入なのでしょうが
趣味程度ならflexmojos-maven-pluginで十分なのでしょうかね?
むむ~
flexmojos-maven-pluginのソース読む元気ないし
FlashBuilderの安いものなら3万円くらいなので買おうかしら?

参考URL
http://blog.flex-mojos.info/
http://flexmojos.sonatype.org/getting-started.html
https://docs.sonatype.org/display/FLEXMOJOS/Home
https://docs.sonatype.org/display/FLEXMOJOS/Building+an+AIR+Application
https://repository.sonatype.org/content/sites/maven-sites/flexmojos/latest/plugin-info.html
http://code.google.com/p/jianwikis/wiki/MavenForFlexAirProject#Flex_AIR_Maven_Project