小编She*_*jie的帖子

Maven项目版本继承 - 我是否必须指定父版本?

我有两个项目:父项目:A,子项目:B

A/pom.xml中:

<groupId>com.dummy.bla</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
Run Code Online (Sandbox Code Playgroud)

在B/pom.xml中,我有:

    <parent>
        <groupId>com.dummy.bla</groupId>
        <artifactId>parent</artifactId>
        <version>0.1-SNAPSHOT</version>     
    </parent>

    <groupId>com.dummy.bla.sub</groupId>
    <artifactId>kid</artifactId>
Run Code Online (Sandbox Code Playgroud)

我希望B从父级继承该版本,所以我需要放置的唯一位置0.1-SNAPSHOTA/pom.xml.但是,如果删除了<version>0.1-SNAPSHOT</version>B/pom.xml下父节,Maven的抱怨缺少的版本父.

有没有办法可以使用${project.version}或类似的东西,以避免01.-SNAPSHOT在两个poms?

inheritance maven

161
推荐指数
7
解决办法
15万
查看次数

用参数模拟构造函数

我有一个课程如下:

public class A {
    public A(String test) {
        bla bla bla
    }

    public String check() {
        bla bla bla
    }
}
Run Code Online (Sandbox Code Playgroud)

在构造函数中的逻辑A(String test)check()是我试图嘲弄的事情.我想要任何调用:new A($$$any string$$$).check()返回一个虚拟字符串"test".

我试过了:

 A a = mock(A.class); 
 when(a.check()).thenReturn("test");

 String test = a.check(); // to this point, everything works. test shows as "tests"

 whenNew(A.class).withArguments(Matchers.anyString()).thenReturn(rk);
 // also tried:
 //whenNew(A.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(rk);

 new A("random string").check();  // this doesn't work
Run Code Online (Sandbox Code Playgroud)

但它似乎没有奏效.new A($$$any string$$$).check()仍然通过构造函数逻辑而不是获取模拟对象A.

java junit mocking mockito powermock

74
推荐指数
5
解决办法
20万
查看次数

杰克逊 - 最好的方法是将一个java列表写入json数组

我想使用jackson将ArrayList转换为JsonArray.

Event.java:这是java bean类,有两个字段"field1","field2"映射为JsonProperty.

我的目标是:

兑换

ArrayList<Event> list = new ArrayList<Event>();
    list.add(new Event("a1","a2"));
    list.add(new Event("b1","b2"));
Run Code Online (Sandbox Code Playgroud)

[
{"field1":"a1", "field":"a2"},
{"field1":"b1", "field":"b2"}
]
Run Code Online (Sandbox Code Playgroud)

我能想到的是: writeListToJsonArray():

public void writeListToJsonArray() throws IOException {  
    ArrayList<Event> list = new ArrayList<Event>();
    list.add(new Event("a1","a2"));
    list.add(new Event("b1","b2"));

    OutputStream out = new ByteArrayOutputStream();

    JsonFactory jfactory = new JsonFactory();
    JsonGenerator jGenerator = jfactory.createJsonGenerator(out, JsonEncoding.UTF8);
    ObjectMapper mapper = new ObjectMapper();
    jGenerator.writeStartArray(); // [

    for (Event event : list) {
        String e = mapper.writeValueAsString(event);
        jGenerator.writeRaw(usage);
        // here, big hassles to write …
Run Code Online (Sandbox Code Playgroud)

java json jackson

51
推荐指数
3
解决办法
11万
查看次数

sed - 如何使用sed进行正则表达式组

无论如何你可以使用sed像java正则表达式模式/匹配/组一样做正则表达式匹配组吗?

如果我有字符串

test-artifact-201251-balbal-0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

我如何使用sed只是为了得到如下结果:

test-artifact-0.1-SNASHOT.jar
Run Code Online (Sandbox Code Playgroud)

我想知道sed允许你做一些像java正则表达式的东西,你定义的模式如下:

([a-z]*-[a-z]*-)([0-9]*-)([a-z]*-)([.]*SNAPSHOT.jar)
Run Code Online (Sandbox Code Playgroud)

然后你可以得到如下数组的结果:

test-artifact-
201251-
balbal-
0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

regex linux sed

37
推荐指数
3
解决办法
6万
查看次数

Maven build [警告]我们有一个重复的类

有谁知道我的maven构建发生了什么?我收到了很多重复的警告.

[WARNING] We have a duplicate org/apache/commons/logging/impl/LogFactoryImpl$1.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/LogFactoryImpl.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/NoOpLog.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/SimpleLog$1.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/SimpleLog.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/Jdk14Logger.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
Run Code Online (Sandbox Code Playgroud)

我查看了我当地的m2 repo,我在commons-logging-api jar,LogFactoryImpl.class和LogFactoryImpl $ 1.class中有两个类.与警告中提到的所有类相同.

有一点需要提一下,我在我的pom.xml中使用了shade插件.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers> …
Run Code Online (Sandbox Code Playgroud)

build maven maven-shade-plugin apache-commons-logging

28
推荐指数
3
解决办法
3万
查看次数

python - lambda可以有多个返回

我知道lambda没有返回表达式.一般

def one_return(a):
    #logic is here
    c = a + 1
    return c
Run Code Online (Sandbox Code Playgroud)

可写:

lambda a : a + 1
Run Code Online (Sandbox Code Playgroud)

怎么样在lambda函数中写这个:

def two_returns(a, b):
    # logic is here
    c = a + 1
    d = b * 1
    return c, d
Run Code Online (Sandbox Code Playgroud)

python lambda tuples

27
推荐指数
3
解决办法
2万
查看次数

类路径引用的存档不存在

我的日食是Indigo Java classic.

我有一个java项目,它有mockito-all作为依赖项.pom.xml中:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.8.5</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

mvn clean install,一切都好.然后我做mvn eclipse:eclipse了解决Eclipse中的所有依赖项.

当我尝试在eclipse中运行Junit时,它不会运行并给我这个错误:

'Launching YourTest' has encountered a problem.

The archive: /home/shengjie/.m2/repository/org/mockito/mockito-all/1.9.5.jar which is referenced by the classpath, does not exist.
Run Code Online (Sandbox Code Playgroud)

我的项目pom.xml声称它取决于mockito-all 1.8.5,我不确定1.9.5引用的来源.有任何想法吗?

== ==编辑

$ mvn dependency:tree | grep mockito
[INFO] +- org.mockito:mockito-all:jar:1.8.5:test
[INFO] \- org.powermock:powermock-api-mockito:jar:1.4.12:test
[INFO] \- org.powermock:powermock-api-mockito:jar:1.4.12:test
[INFO]    +- org.mockito:mockito-all:jar:1.8.5:test (version managed from 1.9.0)
[INFO] |  +- org.mockito:mockito-all:jar:1.8.5:test (version managed from 1.9.5; scope managed from compile)
[INFO] |  +- org.mockito:mockito-all:jar:1.8.5:test (version …
Run Code Online (Sandbox Code Playgroud)

java eclipse pom.xml maven

25
推荐指数
3
解决办法
8万
查看次数

java注释可以像HashMap一样具有复杂的返回类型

注释可以具有复杂的返回类型,例如HashMap.

我正在寻找类似的东西:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface column {
    public HashMap<String, String> table();
}
Run Code Online (Sandbox Code Playgroud)

所以我可以有一个常量注释(伪代码):

@column({table=(dbName, tableName), table=(dbName, tableName2)})
public static final String USER_ID = "userid";
Run Code Online (Sandbox Code Playgroud)

如果Annotation不允许你有复杂的返回类型,那么对于这种情况有什么好的做法吗?

java collections annotations

24
推荐指数
2
解决办法
2万
查看次数

如何一次性将文件内容读入变量?

在Java中,如果您确定某个文件非常小,您可以使用readBytes()方法一次性读取内容,而不是逐行读取或使用缓冲区.

只是想知道shell脚本,我知道我们可以这样做:

    while read line
    do
      echo $line
      LINE = $line
    done < "test.file"
    echo $LINE
Run Code Online (Sandbox Code Playgroud)

如果我的test.file是这样的:

testline1
testline2
testline3
Run Code Online (Sandbox Code Playgroud)

这只给了我最后一行$LINE.$LINE包含"testline3".

我的问题是:如何将多行读入整个文件放入一个变量中,这样我才能得到$LINE="testline1\ntestline2\ntestline3"

bash shell

22
推荐指数
3
解决办法
8万
查看次数

powermock mocking构造函数通过whennew()不能与匿名类一起使用

我有一个DummyResource类和一个DummyTarget文件,以及一个测试类TestDummyResource,如下所示,但是DummyResource dr = mock(DummyResource.class)当我在普通类中调用构造函数时,模拟对象才起作用,当它在匿名类中调用时,它调用实际的构造函数而不是使用被模拟的对象.

版本:

powermock 1.4.12 mockito 1.9.0 junit 4.8.2

DummyTarget.java:

import java.io.IOException;
import java.io.OutputStream;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;


public class DummyTarget {
    public StreamingOutput testMocking() {
        return new StreamingOutput() {
            @Override
            public void write(OutputStream arg0) throws IOException, WebApplicationException {
                new DummyResource();
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

DummyResource.java:

package com.smin.dummy;

public class DummyResource {
    public DummyResource() {
        System.out.println("mock failure");
    }
}
Run Code Online (Sandbox Code Playgroud)

TestDummyResource.java:

package com.smin.dummy;

import static org.mockito.Mockito.mock;

import java.io.IOException;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;

import org.junit.Before;
import org.junit.Test;
import …
Run Code Online (Sandbox Code Playgroud)

java junit mockito powermock

21
推荐指数
3
解决办法
4万
查看次数