4

我尝试使用 Maven 构建带有 Android 注释的项目。这是我的构建部分:

<build>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>${project.basedir}</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}/filtered-manifest</targetPath>
            <includes>
                <include>AndroidManifest.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>parse-version</id>
                    <goals>
                        <goal>parse-version</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <extensions>true</extensions>
            <configuration>
                <assetsDirectory>assets</assetsDirectory>
                <resourceDirectory>res</resourceDirectory>
                <sdk>
                    <path>${env.ANDROID_HOME}</path>
                    <platform>15</platform>
                </sdk>
                <undeployBeforeDeploy>true</undeployBeforeDeploy>
                <sign>
                    <debug>false</debug>
                </sign>
                <androidManifestFile>${project.build.directory}/filtered-manifest/AndroidManifest.xml</androidManifestFile>
            </configuration>

            <executions>
                <execution>
                    <id>update-manifest</id>
                    <goals>
                      <goal>manifest-update</goal>
                    </goals>
                    <configuration>
                      <manifest>
                        <versionName>${project.version}</versionName>
                        <debuggable>${androidmanifest.debuggable}</debuggable>
                        <versionCode>${parsedVersion.incrementalVersion}</versionCode>
                      </manifest>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <includes>
                    <include>${project.basedir}/target/generated-sources/**</include>
                </includes>
            </configuration>
            <extensions>true</extensions>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.0.5</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>com.googlecode.androidannotations.AndroidAnnotationProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
            <dependencies/>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jarsigner-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <id>signing</id>
                    <goals>
                        <goal>sign</goal>
                        <goal>verify</goal>
                    </goals>
                    <phase>package</phase>
                    <inherited>true</inherited>
                    <configuration>
                        <removeExistingSignatures>true</removeExistingSignatures>
                        <archiveDirectory />
                        <includes>
                            <include>${project.build.directory}/${project.artifactId}-${version}.apk</include>
                        </includes>
                        <keystore>${basedir}/certificate/release-key.keystore</keystore>
                        <alias></alias>
                        <storepass></storepass>
                        <verbose>false</verbose>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <tagBase>scm:git:${staging.git.repo.url}</tagBase>
                <scmCommentPrefix>release:</scmCommentPrefix>
                <preparationGoals>clean install</preparationGoals>
                <arguments>-Dstaging.git.repo.url=${staging.git.repo.url}</arguments>
                <checkModificationExcludes>
                    <checkModificationExclude>.gitignore</checkModificationExclude>
                    <checkModificationExclude>.classpath</checkModificationExclude>
                    <checkModificationExclude>.project</checkModificationExclude>
                    <checkModificationExclude>.settings</checkModificationExclude>
                    <checkModificationExclude>**.iml</checkModificationExclude>
                    <checkModificationExclude>.idea**</checkModificationExclude>
                    <checkModificationExclude>project.properties</checkModificationExclude>
                    <checkModificationExclude>default.properties</checkModificationExclude>
                </checkModificationExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

这是我得到的一段构建日志:

[INFO] ------------------------------------------------------------------------
[INFO] Building SomeApp 1.03.15-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- build-helper-maven-plugin:1.7:parse-version (parse-version) @ SomeApp ---
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default) @ SomeApp ---
[debug] execute contextualize
[INFO] Using 'utf-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to /Users/darja/Works/someapp/Android/target/filtered-manifest
[INFO] 
[INFO] --- android-maven-plugin:3.2.0:generate-sources (default-generate-sources) @ SomeApp ---
[INFO] ANDROID-904-002: Found aidl files: Count = 0
[INFO] ANDROID-904-002: Found aidl files: Count = 0
[INFO] /Users/darja/Development/sdk/platform-tools/aapt [package, -m, -J, /Users/darja/Works/someapp/Android/target/generated-sources/r, -M, /Users/darja/Works/someapp/Android/target/filtered-manifest/AndroidManifest.xml, -S, /Users/darja/Works/someapp/Android/res, --auto-add-overlay, -A, /Users/darja/Works/someapp/Android/assets, -I, /Users/darja/Development/sdk/platforms/android-15/android.jar]
[INFO] 
[INFO] --- maven-processor-plugin:2.0.5:process (process) @ Someapp ---
[INFO] Source directory: /Users/darja/Works/someapp/Android/target/generated-sources/apt added
[INFO] javac option: -cp
[INFO] javac option: /Users/darja/.m2/repository/org/sonatype/sisu/sisu-inject-bean/1.4.3.1/sisu-inject-bean-1.4.3.1.jar:/Users/darja/.m2/repository/org/sonatype/sisu/sisu-guice/2.9.1/sisu-guice-2.9.1-noaop.jar:/Users/darja/.m2/repository/org/sonatype/aether/aether-util/1.8/aether-util-1.8.jar:/Users/darja/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar:/Users/darja/.m2/repository/org/codehaus/plexus/plexus-utils/2.0.4/plexus-utils-2.0.4.jar:/Users/darja/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar:/Users/darja/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar:/Users/darja/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar:/Users/darja/Works/someapp/Android:/Users/darja/Works/someapp/Android/target/generated-sources/extracted-dependencies/src/main/resources:/Users/darja/Works/someapp/Android/target/classes:/Users/darja/.m2/repository/com/google/android/android/4.0.1.2/android-4.0.1.2.jar:/Users/darja/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/Users/darja/.m2/repository/org/apache/httpcomponents/httpclient/4.0.1/httpclient-4.0.1.jar:/Users/darja/.m2/repository/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar:/Users/darja/.m2/repository/commons-codec/commons-codec/1.3/commons-codec-1.3.jar:/Users/darja/.m2/repository/org/khronos/opengl-api/gl1.1-android-2.1_r1/opengl-api-gl1.1-android-2.1_r1.jar:/Users/darja/.m2/repository/xerces/xmlParserAPIs/2.6.2/xmlParserAPIs-2.6.2.jar:/Users/darja/.m2/repository/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar:/Users/darja/.m2/repository/org/json/json/20080701/json-20080701.jar:/Users/darja/.m2/repository/com/bugsense/BugSense/3.0.7/BugSense-3.0.7.jar:/Users/darja/.m2/repository/com/googlecode/androidannotations/androidannotations/2.6/androidannotations-2.6.jar:/Users/darja/.m2/repository/com/googlecode/androidannotations/androidannotations/2.6/androidannotations-2.6-api.jar:/Users/darja/.m2/repository/com/google/gcm/gcm/1.0.2/gcm-1.0.2.jar:/Users/darja/.m2/repository/joda-time/joda-time/2.1/joda-time-2.1.jar:
[INFO] javac option: -proc:only
[INFO] javac option: -processor
[INFO] javac option: com.googlecode.androidannotations.AndroidAnnotationProcessor
[INFO] javac option: -d
[INFO] javac option: /Users/darja/Works/someapp/Android/target/classes
[INFO] javac option: -s
[INFO] javac option: /Users/darja/Works/someapp/Android/target/generated-sources/apt
[INFO] diagnostic /Users/darja/Works/someapp/Android/src/com/someapp/social/facebook/FbDialog.java:37: cannot find symbol
symbol  : class R
location: package com.someapp

...Many complains about cannot find class R...

[INFO] diagnostic /Users/darja/Works/someapp/Android/src/com/someapp/ui/views/SearchView.java:47: package R does not exist
[INFO] diagnostic Note: Starting AndroidAnnotations annotation processing
[INFO] diagnostic Note: Dummy source file: file:///Users/darja/Works/someapp/Android/target/generated-sources/apt/dummy1358938267097.java
[INFO] diagnostic Note: AndroidManifest.xml file found: /Users/darja/Works/someapp/Android/AndroidManifest.xml
[INFO] diagnostic warning: The AndroidManifest.xml file was found, but not the compiled R class: com.someapp.R
[INFO] diagnostic error: Unexpected error. Please report an issue on AndroidAnnotations, with the following content: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.googlecode.androidannotations.helper.TargetAnnotationHelper.extractAnnotationValue(TargetAnnotationHelper.java:52)
    at com.googlecode.androidannotations.helper.TargetAnnotationHelper.extractAnnotationValue(TargetAnnotationHelper.java:40)
    at com.googlecode.androidannotations.helper.IdValidatorHelper.idExists(IdValidatorHelper.java:47)
    at com.googlecode.androidannotations.helper.IdValidatorHelper.idExists(IdValidatorHelper.java:42)
    at com.googlecode.androidannotations.validation.EActivityValidator.validate(EActivityValidator.java:54)
    at com.googlecode.androidannotations.validation.ModelValidator.validate(ModelValidator.java:53)
    at com.googlecode.androidannotations.AndroidAnnotationProcessor.validateAnnotations(AndroidAnnotationProcessor.java:389)
    at com.googlecode.androidannotations.AndroidAnnotationProcessor.processThrowing(AndroidAnnotationProcessor.java:341)
    at com.googlecode.androidannotations.AndroidAnnotationProcessor.process(AndroidAnnotationProcessor.java:318)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:627)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:556)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:701)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:987)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
    at com.sun.tools.javac.main.Main.compile(Main.java:353)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)
    at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.executeWithExceptionsHandled(AbstractAnnotationProcessorMojo.java:350)
    at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.execute(AbstractAnnotationProcessorMojo.java:197)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.googlecode.androidannotations.helper.TargetAnnotationHelper.extractAnnotationValue(TargetAnnotationHelper.java:50)
    ... 38 more
Caused by: java.lang.annotation.AnnotationTypeMismatchException: Incorrectly typed data found for annotation element public abstract int com.googlecode.androidannotations.annotations.EActivity.value() (Found data of type int)
    at com.sun.tools.javac.model.AnnotationProxyMaker$ValueVisitor$1.generateException(AnnotationProxyMaker.java:243)
    at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:57)
    at $Proxy16.value(Unknown Source)
    ... 43 more

但是我看到R.java是在target/generated-source/r/com/someapp(包名是com.someapp)中生成的我做错了什么?

可能有人有适用于 Android 注释项目的 Maven 脚本并且可以共享它吗?

4

2 回答 2

1

您可以尝试配置maven处理器插件的sourcedirectory:

<plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <version>2.0.5</version>
        <executions>
          <execution>
            <id>process</id>
            <goals>
              <goal>process</goal>
            </goals>
            <phase>process-sources</phase>
            <configuration>
              <sourceDirectory>the path of your target dir</sourceDirectory>
              <processors>
                <processor>com.googlecode.androidannotations.AndroidAnnotationProcessor</processor>
              </processors>
            </configuration>
          </execution>
        </executions>
        <dependencies />
      </plugin>
于 2013-01-23T14:14:51.050 回答
1

正确的方法是使用来自Android KickstartR的 Maven 配置

于 2013-04-29T07:11:02.720 回答