使用AppFuse 2需注意的一个事项:一个模型类的名称不能是另一个的前缀
在我的范例工程中有如下类:
import
javax.persistence.
*
;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@Entity
public class Product extends BaseObject {
private static final long serialVersionUID = - 7166739807708948462L ;
private Long id;
private String code;
private String name;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this .id = id;
}
@Column(nullable = false , length = 50 , unique = true )
public String getCode() {
return code;
}
public void setCode(String code) {
this .code = code;
}
@Column(nullable = false , length = 50 )
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@Override
public int hashCode() {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ((code == null ) ? 0 : code.hashCode());
result = prime * result + ((id == null ) ? 0 : id.hashCode());
result = prime * result + ((name == null ) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj)
return true ;
if (obj == null )
return false ;
if (getClass() != obj.getClass())
return false ;
final Product other = (Product) obj;
if (code == null ) {
if (other.code != null )
return false ;
} else if ( ! code.equals(other.code))
return false ;
if (id == null ) {
if (other.id != null )
return false ;
} else if ( ! id.equals(other.id))
return false ;
if (name == null ) {
if (other.name != null )
return false ;
} else if ( ! name.equals(other.name))
return false ;
return true ;
}
@Override
public String toString() {
ToStringBuilder sb = new ToStringBuilder( this , ToStringStyle.DEFAULT_STYLE)
.append( " code " , this .code)
.append( " name " , this .name);
return sb.toString();
}
}
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@Entity
public class Product extends BaseObject {
private static final long serialVersionUID = - 7166739807708948462L ;
private Long id;
private String code;
private String name;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this .id = id;
}
@Column(nullable = false , length = 50 , unique = true )
public String getCode() {
return code;
}
public void setCode(String code) {
this .code = code;
}
@Column(nullable = false , length = 50 )
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@Override
public int hashCode() {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ((code == null ) ? 0 : code.hashCode());
result = prime * result + ((id == null ) ? 0 : id.hashCode());
result = prime * result + ((name == null ) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj)
return true ;
if (obj == null )
return false ;
if (getClass() != obj.getClass())
return false ;
final Product other = (Product) obj;
if (code == null ) {
if (other.code != null )
return false ;
} else if ( ! code.equals(other.code))
return false ;
if (id == null ) {
if (other.id != null )
return false ;
} else if ( ! id.equals(other.id))
return false ;
if (name == null ) {
if (other.name != null )
return false ;
} else if ( ! name.equals(other.name))
return false ;
return true ;
}
@Override
public String toString() {
ToStringBuilder sb = new ToStringBuilder( this , ToStringStyle.DEFAULT_STYLE)
.append( " code " , this .code)
.append( " name " , this .name);
return sb.toString();
}
}
这个类没有什么特别的地方,但运行mvn appfuse:gen -Dentity=Product为它生成CRUD相关的代码时却如下错误:
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] E:\USR\openstore\target\appfuse\generated-sources\src\test\resources\Product-sample-data.xml doesn't exist
[INFO] ------------------------------------------------------------------------
[INFO] Trace
E:\USR\openstore\target\appfuse\generated-sources\src\test\resources\Product-sample-data.xml doesn't exist
at org.apache.tools.ant.taskdefs.LoadResource.execute(LoadResource.java:142)
at org.appfuse.tool.ArtifactInstaller.installSampleData(ArtifactInstaller.java:135)
at org.appfuse.tool.ArtifactInstaller.execute(ArtifactInstaller.java:47)
at org.appfuse.mojo.exporter.AppFuseGeneratorMojo.doExecute(AppFuseGeneratorMojo.java:262)
at org.appfuse.mojo.HibernateExporterMojo.execute(HibernateExporterMojo.java:138)
at org.appfuse.mojo.exporter.AppFuseGeneratorMojo.execute(AppFuseGeneratorMojo.java:202)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:447)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:493)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:463)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:311)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:278)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:143)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:333)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:126)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:282)
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:585)
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)
[INFO] ------------------------------------------------------------------------
[INFO] E:\USR\openstore\target\appfuse\generated-sources\src\test\resources\Product-sample-data.xml doesn't exist
[INFO] ------------------------------------------------------------------------
[INFO] Trace
E:\USR\openstore\target\appfuse\generated-sources\src\test\resources\Product-sample-data.xml doesn't exist
at org.apache.tools.ant.taskdefs.LoadResource.execute(LoadResource.java:142)
at org.appfuse.tool.ArtifactInstaller.installSampleData(ArtifactInstaller.java:135)
at org.appfuse.tool.ArtifactInstaller.execute(ArtifactInstaller.java:47)
at org.appfuse.mojo.exporter.AppFuseGeneratorMojo.doExecute(AppFuseGeneratorMojo.java:262)
at org.appfuse.mojo.HibernateExporterMojo.execute(HibernateExporterMojo.java:138)
at org.appfuse.mojo.exporter.AppFuseGeneratorMojo.execute(AppFuseGeneratorMojo.java:202)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:447)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:493)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:463)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:311)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:278)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:143)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:333)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:126)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:282)
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:585)
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)
仔细查看了Product的源码,没有什么错啊......配置文件也看不出有什么问题......其它类都能正常地生成代码,就这个类死活不行。
注意到运行mvn appfuse:remove -Dentity=Product的过程中有如下提示:
[info] [AppFuse] Removing generated files (pattern: **/Product*.java)
[info] [AppFuse] Removing sample data for DbUnit
[info] [AppFuse] Removing Spring bean definitions
[info] [AppFuse] Removing Struts views and configuring
[info] [AppFuse] Removing generated files (pattern: **/model/*.xml)
[info] [AppFuse] Removing generated files (pattern: **/webapp/action/*.xml)
[info] [AppFuse] Removing generated files (pattern: Product*.jsp)
[info] [AppFuse] Removing sample data for DbUnit
[info] [AppFuse] Removing Spring bean definitions
[info] [AppFuse] Removing Struts views and configuring
[info] [AppFuse] Removing generated files (pattern: **/model/*.xml)
[info] [AppFuse] Removing generated files (pattern: **/webapp/action/*.xml)
[info] [AppFuse] Removing generated files (pattern: Product*.jsp)
其中多处有Product*字样。想到项目中还有另一个类ProductCategory(已经为它生成了代码),是不是两个类之间有什么冲突?于是做如下尝试:
1.备份Product.java和ProductCategory.java
2.运行mvn appfuse:remove -Dentity=ProductCategory删除已经生成的ProductCategory相关的代码
3.运行mvn appfuse:remove -Dentity=Product删除残留的Product相关的代码
4.恢复Product.java和ProductCategory.java到原目录中
5.将类ProductCategory改名为Category,相应地将文件ProductCategory.java改名为Category.java
6.运行mvn appfuse:gen -Dentity=Category
7.运行mvn appfuse:gen -Dentity=Product
以上所有的步骤均执行成功!表明对于AppFuse的maven plugin来说,类Product和ProductCategory之间确实存在冲突。问题虽然可以绕过,但涉及的两个类的名称都是合法的Java类名,这不能不说是AMP的一个缺陷,我已经在官方论坛上发了一个帖子,希望作者能尽快修复