5

我正在使用 maven 构建我的 java 项目,但没有成功。那么,谁能告诉我 pom.xml 中构建项目所需的基本标签及其对应的值?另外,如何从命令行手动添加风暴依赖项?

我的项目目录结构:

ROOT
 |
 |----com--->test----->newpackage---->*.class
 |--- META-INF------>MANIFEST.MF
 |----resource----->words.txt
 |----pom.xml
 |-----*.jar

最初我尝试了Maven Link并创建了pom.xml这样的:

<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/xsd/maven-4.0.0.xsd">
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <phase>com.test.newpackage</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

但是,在那之后我使用mvn package. 它抛出这个异常:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error building POM (may not be this project's POM).


Project ID: unknown
POM Location: /home/naresh/Desktop/SharedFolder/StormEclipse/pom.xml

Reason: Not a v4.0.0 POM. for project unknown at /home/naresh/Desktop/SharedFolder/StormEclipse/pom.xml


[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.reactor.MavenExecutionException: Not a v4.0.0 POM. for project unknown at /home/naresh/Desktop/SharedFolder/StormEclipse/pom.xml
    at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:404)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:272)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.project.InvalidProjectModelException: Not a v4.0.0 POM. for project unknown at /home/naresh/Desktop/SharedFolder/StormEclipse/pom.xml
    at org.apache.maven.project.DefaultMavenProjectBuilder.readModel(DefaultMavenProjectBuilder.java:1599)
    at org.apache.maven.project.DefaultMavenProjectBuilder.readModel(DefaultMavenProjectBuilder.java:1571)
    at org.apache.maven.project.DefaultMavenProjectBuilder.buildFromSourceFileInternal(DefaultMavenProjectBuilder.java:506)
    at org.apache.maven.project.DefaultMavenProjectBuilder.build(DefaultMavenProjectBuilder.java:200)
    at org.apache.maven.DefaultMaven.getProject(DefaultMaven.java:604)
    at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:487)
    at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:391)
    ... 12 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Mon Jul 22 14:38:24 IST 2013
[INFO] Final Memory: 1M/15M
[INFO] ------------------------------------------------------------------------

之后,我使用了这个链接Maven 链接 2并像这样创建了 pom.xml:

 <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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test.newpackage</groupId>
    <artifactId>wordcount</artifactId>
    <version>0.0.1</version>
    <build>
        <plugins>
            <plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>  
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>com.test.newpackage</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>
        </plugins>
    </build>
    <repositories>
        <!-- Repository where we can found the storm dependencies <repository> 
            <id>clojars.org</id> <url>http://clojars.org/repo</url> </repository> -->
    </repositories>
    <dependencies>
        <!-- Storm Dependency -->
        <dependency>
            <groupId>storm</groupId>
            <artifactId>storm</artifactId>
            <version>0.8.1</version>
        </dependency>
    </dependencies>
</project>

这次它抛出了这个异常:

[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.

Missing:
----------
1) storm:storm:jar:0.8.1

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=storm -DartifactId=storm -Dversion=0.8.1 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=storm -DartifactId=storm -Dversion=0.8.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency: 
    1) com.test.newpackage:wordcount:jar:0.0.1
    2) storm:storm:jar:0.8.1

----------
1 required artifact is missing.

for artifact: 
  com.test.newpackage:wordcount:jar:0.0.1

from the specified remote repositories:
  central (http://repo1.maven.org/maven2)
4

3 回答 3

4

关于你的结构:

Maven 假定 java 类位于src/main/java其中,资源(文件夹resource和 META-INF 目录中的任何内容)位于src/main/resources. 由于根目录中的 java 类(递归),即使您稍微调整一下 maven,您描述的目录结构也将不起作用。为什么不使用默认结构?你真的应该有充分的理由来改变这一点。

风暴依赖:

您已经在 POM 中获得了指向外部存储库的链接。但它被淘汰了。这应该按预期工作。如果您使用代理,则必须在 settings.xml 中定义代理

于 2013-07-22T10:13:24.150 回答
1

你应该至少有这 3 个标签:

<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>

这是 maven 使用这 3 个属性(当您定义依赖项时)识别工件的方式。

于 2013-07-22T09:25:54.863 回答
0

最重要的标签是错误消息中提到的标签:

<modelVersion>4.0.0</modelVersion>

你在你的 pom 文件中错过了。

于 2013-07-22T09:31:48.940 回答