首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在maven android构建过程中,基类java类仍然在类路径中。

在maven android构建过程中,基类java类仍然在类路径中。
EN

Stack Overflow用户
提问于 2012-06-28 17:59:38
回答 1查看 603关注 0票数 0

我遇到了一个相当奇怪的问题,因为android java实现与sun java实现不同,而且在android项目的maven构建过程中,基类java类仍然包含在类路径中(一直到最后)。我认为解决方案是在类路径上不包含java类,但我似乎找不到这样的方法。

基本上,在java.util.concurrent中有一个名为java.util.concurrent的类(在android和java中)。java类包含两个名为newTaskFor的方法,我想在android中使用这些方法,但是android实现没有它们。没问题,我只是实现它们(通过一些更改,比如使用未来而不是RunnableFuture)。这在ant (我们正在迁移的构建工具)中工作得很好。

问题是,当maven试图编译我的类(它扩展了AbstractExecutorService),而不是只是在没有找到的时候将我的方法添加到android实现中,它会继续沿着类路径,找到java方法,并抱怨返回类型不匹配。理想情况下,我认为java类甚至不应该在android构建过程中可用,因为它们所能做的就是让您认为您可以运行实际上无法运行的方法,但是目前您可以运行android中无法使用的各种java方法,并且它会编译得很好(在maven中)。

有没有人对此有任何建议或解决方案?有人遇到类似的事吗?

编辑:以下是pom的相关部分(省略存储库等):

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.beanshell</groupId>
        <artifactId>bsh</artifactId>
        <version>2.0b5</version>
        <optional>true</optional>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <optional>true</optional>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android-with-java</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.byteman</groupId>
        <artifactId>byteman-bmunit</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bcprov-jdk15</artifactId>
        <version>140</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>conf</directory>
            <includes>
                <include>*.xml</include>
            </includes>
            <excludes>
                <exclude>*-service.xml</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>${project.build.directory}/schema</directory>
        </resource>
        <resource>
           <directory>${project.basedir}</directory>
           <includes>
              <include>INSTALL.html</include>
              <include>LICENSE</include>
              <include>README</include>
           </includes>
        </resource>
        <resource>
          <directory>${project.basedir}/lib</directory>
          <includes>
             <include>licenses/thirdparty*</include>
          </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <excludes>
                    <exclude>stuff/stuff/util/JUnitXMLReporter.java</exclude>
                </excludes>
            </configuration>
        </plugin>
       <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <version>1.7</version>
          <executions>
             <execution>
                <id>add-source</id>
                <phase>validate</phase>
                <goals>
                   <goal>add-source</goal>
                </goals>
                <configuration>
                   <sources>
                      <source>target/generated-sources</source>
                   </sources>
                </configuration>
             </execution>
             <execution>
                <id>add-test-source</id>
                <phase>validate</phase>
                <goals>
                   <goal>add-test-source</goal>
                </goals>
             </execution>
          </executions>
       </plugin>
       <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <phase>process-resources</phase>
                  <configuration>
                    <tasks>
                        <echo>Precompiling magic ids and protocol ids</echo>    
                        <xslt in="conf/stuff.xml" out="target/generated-sources/stuff/stuff/ClassMagicEncoding.java"
                            style="conf/stuff.xslt">
                            <param name="type" expression="Magic"/>
                        </xslt>
                        <xslt in="conf/stuff.xml" out="target/generated-sources/stuff/stuff/ClassProtocolEncoding.java"
                            style="conf/stuff.xslt">
                            <param name="type" expression="Protocol"/>
                        </xslt>
                    </tasks>
                  </configuration>
                </execution>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <property name="compile_classpath" refid="maven.compile.classpath"/>
                            <property name="plugin_classpath" refid="maven.plugin.classpath"/>
                            <delete dir="${project.build.directory}/schema" failonerror="false"/>
                            <mkdir dir="${project.build.directory}/schema"/>
                            <java classname="stuff.stuff.util.XMLSchemaGenerator">
                                <classpath>
                                    <pathelement path="${compile_classpath}"/>
                                    <pathelement path="${plugin_classpath}"/>
                                </classpath>
                                <arg line="-o ${project.build.directory}/schema"/>
                            </java>
                        </tasks>
                    </configuration>
                </execution>
            </executions>

            <configuration>
                <!-- prints the classpath to ensure correct jars are available -->
                <tasks>
                            <property name="compile_classpath" refid="maven.compile.classpath"/>
                    <echo>rawr=${compile_classpath}</echo>
                    <echo>java.class.path=${java.class.path}</echo>
                    <echo>CLASSPATH=${env.CLASSPATH}</echo>
                </tasks>
            </configuration>

            <dependencies>
                <dependency>
                    <groupId>xalan</groupId>
                    <artifactId>xalan</artifactId>
                    <version>2.7.1</version>
                </dependency>
                <dependency>
                    <groupId>xalan</groupId>
                    <artifactId>serializer</artifactId>
                    <version>2.7.1</version>
                </dependency>
                <dependency>
                    <groupId>ant</groupId>
                    <artifactId>ant-antlr</artifactId>
                    <version>1.6.5</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant-nodeps</artifactId>
                    <version>1.8.1</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Main-Class>stuff.stuff.Version</Main-Class>
                    <Implementation-Version>${project.version}</Implementation-Version>
                    <Export-Package>
                        schema;version=${project.version},
                        ${project.groupId}.*;version=${project.version}
                    </Export-Package>
                    <Bundle-RequiredExecutionEnvironment>J2SE-1.6</Bundle-RequiredExecutionEnvironment>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <extensions>true</extensions>
                <configuration>
                    <sdk>
                         <platform>8</platform>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
        </plugin>
    </plugins>
</build>

Edit2:这是我得到的编译错误.

代码语言:javascript
复制
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mystuff: Compilation failure: Compilation failure:
[ERROR] /home/stuff/stuff/ExecutionService.java:[775,28] <T>newTaskFor(java.lang.Runnable,T) in stuff.ExecutionService cannot override <T>newTaskFor(java.lang.Runnable,T) in java.util.concurrent.AbstractExecutorService; attempting to use incompatible return type
[ERROR] found   : java.util.concurrent.Future<T>
[ERROR] required: java.util.concurrent.RunnableFuture<T>
[ERROR] /home/stuff/ExecutionService.java:[791,28] <T>newTaskFor(java.util.concurrent.Callable<T>) in stuff.ExecutionService cannot override <T>newTaskFor(java.util.concurrent.Callable<T>) in java.util.concurrent.AbstractExecutorService; attempting to use incompatible return type
[ERROR] found   : java.util.concurrent.Future<T>
[ERROR] required: java.util.concurrent.RunnableFuture<T>
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mystuff: Compilation failure

注意: ExecutionService是扩展AbstractExecutorService的类。但是,我希望它只扩展android版本,而不是基本的java版本。我甚至尝试将newTaskFor的存根方法插入到android版本的AbstractExecutorService中(考虑到编译器应该假设我正在覆盖这些方法,而不是基本的java方法--尽管我应该注意到代码中没有@重写),但它仍然没有工作。

没有人要求它们,但下面是我的方法声明:

代码语言:javascript
复制
protected <T> Future<T> newTaskFor(Runnable runnable, T value) 
{
//stuff
}

protected <T> Future<T> newTaskFor(Callable<T> callable) 
{
//stuff
{

我目前的理论是,它与模板类型有关,但我不太确定如何证明或修复它。

编辑6/30:我也应该注意这不是新代码。它已经有一年多的历史了,从那时起就一直在使用ant进行完美的编译。它是许多稳定版本的一部分。我们现在正在将构建过程迁移到maven,因此我正在尝试将此包设置为正确构建。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-05 16:59:13

问题是Maven自动将Oracle JDK放在类路径上,并且Android提供了这些实现的替代实现,它们有时是不同的。在这个阶段,我不知道是否可以告诉Maven Compiler插件从其类路径中删除JDK,只需要使用类路径上的依赖项。这样就解决了这个问题。

请在Android插件问题跟踪器中创建一个问题,并可能通过询问Maven用户或开发人员列表来进行调查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11250352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档