将eclipse项目库添加​​到maven构建路径?

时间:2013-08-27 09:16:34

标签: android maven

我的eclipse工作区里有两个Android项目。

主项目名为Hello,另一个用作库引用的android项目称为HelloLibrary。

我已经在eclipse构建路径中添加了HelloLibrary项目,方法是右键单击Hello项目并将HelloLibrary添加为库项目。

Eclipse设法查找,编译和构建现在使用HelloLibrary代码的Hello项目,但是当我尝试使用以下代码构建和运行测试时,它无法找到HelloLibrary项目:

mvn clean test -e

完整堆栈跟踪如下:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project Hello: Compilation failure: Compilation failure:
[ERROR] /Users/Jonathan/Android work/Hello/src/test/java/com/example/hello/TestFullscreenActivity.java:[9,32] package com.example.hellolibrary does not exist
[ERROR] /Users/xxx/Android work/Hello/src/test/java/com/example/hello/TestFullscreenActivity.java:[30,17] cannot find symbol
[ERROR] symbol  : class Lib
[ERROR] location: class test.java.com.example.hello.TestFullscreenActivity
[ERROR] /Users/xxx/Android work/Hello/src/test/java/com/example/hello/TestFullscreenActivity.java:[30,31] cannot find symbol
[ERROR] symbol  : class Lib
[ERROR] location: class test.java.com.example.hello.TestFullscreenActivity
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project Hello: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    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:320)
    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: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:858)
    at org.apache.maven.plugin.compiler.TestCompilerMojo.execute(TestCompilerMojo.java:152)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more

我可以将HelloLibrary添加到maven构建路径中,还是需要为HelloLibrary创建maven脚本工件?

两者都是Android项目。

编辑。我将此添加到Hello pom文件

<dependency>
            <groupId>com.example.hellolibrary</groupId>
            <artifactId>HelloLibrary</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>apklib</type>
            <scope>compile</scope>
        </dependency>
由于未找到位于HelloLibrary项目中的Lib类对象

并且测试失败

这是我的测试类

/ **  *考试班  * / @RunWith(RobolectricTestRunner.class) 公共类TestFullscreenActivity {

private String stringOne = "hello";
private String stringTwo = "world";

@Test
public void test() {

    Assert.assertEquals(true, false);

}

@Test
public void testTwo() {

    Lib lib = new Lib();
    Assert.assertEquals(stringOne+stringTwo, lib.combineText(stringOne, stringTwo));

}

@Test
public void testThree() {

    Assert.assertEquals(true, true);

}

}

这是位于HelloLibrary项目中的Lib类

package com.example.hellolibrary;

/**
 * Test lib with some random methods
 * @author
 *
 */
public class Lib {

    public String combineText(String stringOne, String stringTwo){

        String result = "";

        result = stringOne + stringTwo;

        return result;

    }

}

以下是用于HelloLibrary项目的pom文件的上半部分

<?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.example.hellolibrary</groupId>
    <artifactId>HelloLibrary</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>apklib</packaging>
    <name>HelloLibrary</name>

由于

1 个答案:

答案 0 :(得分:1)

在maven依赖项中使用非maven项目有一种解决方法,我们不应该使用它,但是当我们没有选择时,请使用“系统”范围:

<dependency>

  <groupId>org.yourjar</groupId>
  <artifactId>yourjar</artifactId>
  <version>0.1</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/yourjar.jar</systemPath>

</dependency>