Spring项目不会从命令行

时间:2016-06-09 20:24:24

标签: java spring gradle

我不能为我的生活弄清楚如何让我的Spring项目在IDE之外运行。我正在使用Gradle。我上下搜索,包括pply插件:'应用程序'措辞,我看到Jars生成但是当我通过gradlew运行时,它死了说它无法找到文件。

1)我是否需要一个单独的build.gradle文件来运行它?在尝试运行之前,我不得不拿出大量其他项目内容(必须用#删除所有时间)

2)为什么它不起作用?我甚至需要build.gradle文件吗?你如何从最简单的方式从Spring IDE转到在命令行上运行的东西?

apply plugin: 'application'
  dependencies {
    compile project(':caffeine')#
    compile libraries.guava#

    testCompile test_libraries.junit#
    testCompile test_libraries.truth#
    testCompile test_libraries.easymock#
    testCompile test_libraries.guava_testlib#
    compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.13.3'
    compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.13.3'
  }

  jar.manifest {#
    name 'com.github.ben-manes.caffeine.guava'#
    instruction 'Import-Package',#
      'com.google.common.cache',#
      'com.github.benmanes.caffeine.cache',#
      'com.github.benmanes.caffeine.cache.stats'#
    instruction 'Export-Package',#
      'com.github.benmanes.caffeine.guava'#
  }#

  jar {
      baseName = 'gs-gradle'
      version =  '0.1.0'
  }

  tasks.withType(Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
  }
  repositories {
     mavenCentral()
  }
  test {#
    systemProperty 'guava.osgi.version', versions.guava#
    systemProperty 'caffeine.osgi.jar', 
    project(':caffeine').jar.archivePath.path#
    systemProperty 'caffeine-guava.osgi.jar',#    
    project(':guava').jar.archivePath.path#
  }#
  mainClassName = 'org.apache.activemq.store.kahadb.disk.util.DiskMark'

使用已剥离的build.gradle编辑Gradle输出:

Total time: 0.579 secs
    tareks-MacBook-Pro:distributions tarekzegar$ gradlew run --stacktrace
    :compileJava UP-TO-DATE
    :processResources UP-TO-DATE
    :classes UP-TO-DATE
    :run
    Error: Could not find or load main class org.apache.activemq.store.kahadb.disk.util.DiskMark
    :run FAILED

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':run'.
    > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java'' finished with non-zero exit value 

EDIT2: 谢谢你的回复。我拿了disk.util.DiskBenchmark并修改了那个特定的文件,因为我需要添加一个缓存,因此我的名字叫做DiskMark.Java。这是我的DiskMark的前几行

    package org.apache.activemq.store.kahadb.disk.util;

    import java.io.File;
    import java.io.RandomAccessFile;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.Random;
    import java.util.Iterator;

    import com.github.benmanes.caffeine.cache.Caffeine;
    import com.github.benmanes.caffeine.cache.RemovalCause;
    import com.github.benmanes.caffeine.cache.RemovalListener;
    import com.github.benmanes.caffeine.guava.CaffeinatedGuava;
    import com.google.common.cache.Cache;

    import org.apache.activemq.util.RecoverableRandomAccessFile;

    /**
     * This class is used to get a benchmark the raw disk performance.
     */
    public class DiskMark {

    private static final boolean SKIP_METADATA_UPDATE =
    Boolean.getBoolean("org.apache.activemq.file.skipMetadataUpdate");

    boolean verbose;
    // reads and writes work with 4k of data at a time.
   int bs = 1024 * 4;
   // Work with 100 meg file.
   long size = 1024 * 1024 * 500;
   long sampleInterval = 10 * 1000;
   static Cache<Long, byte[]> cache;
   static Callable<byte[]> loader;
   static ArrayList<Long> longIndexList; 
   boolean enableCache = true; 

   public static void main(String[] args) {

我应该采用不同的方法吗?它在IDE中运行良好

编辑3: 我将java文件移动到src / main / java下的新包中,包名为DiskCharacterize,类名为DiskMark。仍然工作。

我使用此build.gradle

构建它
  /**
   * Guava compatibility adapter.
   *
   * The tests are forked from Guava commit e370dde.
   */
  apply plugin: 'application'
  dependencies {
    compile project(':caffeine')
    compile libraries.guava

    testCompile test_libraries.junit
    testCompile test_libraries.truth
    testCompile test_libraries.easymock
    testCompile test_libraries.guava_testlib
    compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.13.3'
    compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.13.3'
  }

  jar.manifest {
    name 'com.github.ben-manes.caffeine.guava'
    instruction 'Import-Package',
      'com.google.common.cache',
      'com.github.benmanes.caffeine.cache',
      'com.github.benmanes.caffeine.cache.stats'
    instruction 'Export-Package',
      'com.github.benmanes.caffeine.guava'
  }


  jar {
      baseName = 'gs-gradle'
      version =  '0.1.0'
  }

  tasks.withType(Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
  }
  repositories {
     mavenCentral()
  }
  test {
    systemProperty 'guava.osgi.version', versions.guava
    systemProperty 'caffeine.osgi.jar', project(':caffeine').jar.archivePath.path
    systemProperty 'caffeine-guava.osgi.jar', project(':guava').jar.archivePath.path
  }
  mainClassName = 'DiskCharacterize.DiskMark'

我切换到存在gs-gradle-0.1.0.jar的/ build / libs /目录。我用这个build.gradle填充这个目录

  /**
   * Guava compatibility adapter.
   *
   * The tests are forked from Guava commit e370dde.
   */
  apply plugin: 'application'
  /*dependencies {
    compile project(':caffeine')
    compile libraries.guava

    testCompile test_libraries.junit
    testCompile test_libraries.truth
    testCompile test_libraries.easymock
    testCompile test_libraries.guava_testlib
    compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.13.3'
    compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.13.3'
  }

  jar.manifest {
    name 'com.github.ben-manes.caffeine.guava'
    instruction 'Import-Package',
      'com.google.common.cache',
      'com.github.benmanes.caffeine.cache',
      'com.github.benmanes.caffeine.cache.stats'
    instruction 'Export-Package',
      'com.github.benmanes.caffeine.guava'
  }*/

  jar {
      baseName = 'gs-gradle'
      version =  '0.1.0'
  }

  tasks.withType(Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
  }
  repositories {
     mavenCentral()
  }
  /*test {
    systemProperty 'guava.osgi.version', versions.guava
    systemProperty 'caffeine.osgi.jar', project(':caffeine').jar.archivePath.path
    systemProperty 'caffeine-guava.osgi.jar', project(':guava').jar.archivePath.path
  }*/
  mainClassName = 'DiskCharacterize.DiskMark'

失败,

    xxxxxx$ gradlew run
    :compileJava UP-TO-DATE
    :processResources UP-TO-DATE
    :classes UP-TO-DATE
    :run
    Error: Could not find or load main class DiskCharacterize.DiskMark
    :run FAILED

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':run'.
    > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

发生了什么事?我需要这个运行,不能为我的生活弄清楚

EDIT4: gradle run不起作用,即使使用apply plugin:添加'java',插件应用程序仍会隐式调用java插件。

然而,我得到了它。我去了../build/distributions/文件夹,里面有guava-2.3.1-SNAPSHOT.tar&amp; guava-2.3.1-SNAPSHOT.zip,我解压缩了zip文件,找到了一个bash文件并简单地执行了它;程序运行。为什么gradle run不起作用我不知道(如果你有任何想法,我仍然真的想明白)。谢谢大家的帮助顺便说一句,谢谢Ben M.

2 个答案:

答案 0 :(得分:0)

基于新信息的编辑答案:

基于Gradle Application Plugin Documentation,run命令针对“主要源集”运行。您尝试运行的类是在src / test下,它不是Gradle默认源位置的一部分(并且基于名称,如果是,则将成为测试源位置的一部分)。请查看Gradle Java Plugin文档(45.2),以便更好地描述源集合以及如何更改它们,而不是在此处给出:)

简短的回答/建议:将你的包和文件从src / test移动到一个名为src / main / java的源文件夹中,以允许Gradle运行以找到它所期望的类(你可以选择重新配置源集,但是对你的案子来说似乎有点过分了)

旧答案(基于错误的Typo试图使用图书馆提供的课程的评估):

  

我假设您正在尝试将课程org.apache.activemq.store.kahadb.disk.util.DiskMark作为主要课程。根据类的名称,我猜你正在尝试使用build.gradle文件中的activemq-kahadb依赖项(而不是自己编写一个与activemq-kahadb结构几乎相同的包结构)< / p>      

对于您所依赖的版本,似乎没有具有该名称的类(基于package tree)。我没有找到任何具有该名称类的ActiveMQ版本。有一个名为org.apache.activemq.store.kahadb.disk.util.DiskBenchmark的类,它包含一个main方法 - 你想要调用的是什么?

答案 1 :(得分:0)

Looking at the DiskMark source you have up and the error message you are getting, the fully qualified name of the DiskMark class (ie, package name + class name) is: org.apache.activemq.store.kahadb.disk.util.DiskMark, not DiskCharacterize.DiskMark. Hence, the class is unable to be found.

There are two solutions:

Solution #1: Tell gradle to look for the right class:

mainClassName = 'org.apache.activemq.store.kahadb.disk.util.DiskMark'

Additionally, as a matter of project organization, the storage location of each file in your project should match the package of your class. In DiskMark's case, the path should be:

%ProjectRoot%/src/main/java/org/apache/activemq/store/kahadb/disk/util/DiskMark.java

Solution #2: Change your class's packaging (and anything else that would need to match)

package DiskCharacterize;
//...
public class DiskMark {
    //...
}

File location:

%projectRoot%/src/main/java/DiskCharacterize/DiskMark.java

This solution is valid, but your current package, DiskCharacterize, does not obey java stylistic conventions (and makes DiskMark look like an inner class to those not familiar with your code).

As a result, I highly suggest combining both solutions and changing your package name to use an all lowercase package name. org.tarek.diskcharacterize would be a very appropriate name for your package.