且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

月蚀:为什么和使用Maven项目没有缺陷的Eclipse项目时,时添加库来构建路径?

更新时间:2023-09-17 16:54:40

随着1.0.0版本的的Andr​​oid对Maven的Eclipse 改变其管理的Maven的类路径的方式。非运行时依赖(实际上,任何事情不是在编译范围内)正在加载到一个新的Maven的,非运行时类路径容器。你看到这些库出现在这个新的类路径容器?

如果不行,请尝试从您的工作空间中删除项目,删除IDE特定的。* 元数据文件,并试图重新导入一个Maven项目。

I have an Android project in Eclipse managed by Maven. Yesterday I upgraded Eclipse and some plugins by the "Checked for Updated" function within Eclipse. After the update has finished, my Android project was defective. First I thought that it's just a "Clean/Update Project/Maven Rebuild" issue or maybe the order of the includes of the libraries didn't fit anymore (for example, Maven dependencies are not on top or something similar), but I was able to build by Maven, so no project or Maven problems, but an Eclipse problem. To get it working again I had to add Hamcrest, Junit and Mockito from the Maven repo to the build path manually. I didn't had to do that before in this project (except android-4.1.1.4.jar). I would like to understand when and why I suddenly have to do that? (In an earlier project I also had to add some external libraries manually).

(I always build and my project by clean install android:deploy android:run.)

Here are some important plugins I have currently installed...

pom.xml

<?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>com.mydomain</groupId>
    <artifactId>myapp</artifactId>
    <packaging>apk</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>My App</name>
    <url>http://maven.apache.org</url>

    <properties>
        <platform.version>2.2.1</platform.version>
        <android.sdk.path>/opt/android-sdk-linux</android.sdk.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <junit.version>4.8.2</junit.version>
        <mockito.version>1.9.5</mockito.version>
        <jackson.version>1.9.13</jackson.version>
        <hamcrest.version>1.3</hamcrest.version>
        <android.version>4.1.1.4</android.version>
        <annotations.version>4.1.1.4</annotations.version>
        <supportv4.version>r13</supportv4.version>
    </properties>

    <repositories>
        <repository>
            <id>codehaus</id>
            <url>http://repository.codehaus.org/org/codehaus</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${android.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>support-v4</artifactId>
            <version>${supportv4.version}</version>
        </dependency>
        <!-- LInt Annotations -->
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>annotations</artifactId>
            <version>${annotations.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Non Android Tests --><!-- hamcrest must be before JUnit due to build errors -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.5.1</version>
        </dependency>


    </dependencies>

    <build>
        <outputDirectory>bin/classes</outputDirectory>
        <testOutputDirectory>bin/test-classes</testOutputDirectory>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jarsigner-plugin</artifactId>
                <version>1.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.5.0</version>
                <configuration>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sign>
                        <debug>true</debug>
                    </sign>
                    <sdk>
                        <platform>13</platform>
                        <path>${android.sdk.path}</path>
                    </sdk>
                    <undeployBeforeDeploy>false</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.5</version>
            </plugin>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                                    <artifactId>android-maven-plugin</artifactId>
                                    <versionRange>[3.2.0,)</versionRange>
                                    <goals>
                                        <goal>manifest-update</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This is the output of the error log

**The problem is that it doesn't find the classes (until I import it manually, but Maven and Eclipse should handle that) If I try to import for example org.junit.Test, org.junit.runner.RunWith or org.mockito.Mock I get in Eclipse

The import xxxxx cannot be resolved

Problems tab in Eclipse:

The project was not built since its build path is incomplete. Cannot find the class file for org.mockito.verification.VerificationMode. Fix the build path then try building this project com.mygame-TRUNK Unknown Java Problem The type org.mockito.verification.VerificationMode cannot be resolved. It is indirectly referenced from required .class files GameProcessorTest.java /com.mygame-TRUNK/src/test/java/com/mygam/game line 1 Java Problem

As of version 1.0.0 Android for Maven Eclipse has changed the way it manages the Maven classpaths. Non-runtime dependencies (effectively, anything not in the compile scope) are now loaded into a new Maven, non-runtime classpath container. Are you seeing these libraries appear in this new classpath container?

If not, please try deleting the project from your workspace, deleting the IDE-specific .* metadata files and trying re-importing as a Maven project.